如何更改uinavigationcontroller后退按钮动作

时间:2012-08-27 07:25:12

标签: iphone objective-c ios

我有UINavigationController并且我必须保留默认后退按钮“后箭头样式”我只想问我是否可以更改后退按钮操作而无需构建新操作并更改其样式

5 个答案:

答案 0 :(得分:1)

没有。如果需要自定义后退按钮,则必须创建自定义UIBarButtonItem,然后将其分配给相应的属性:

self.navigationItem.backBarButtonItem = myCustomBackItem;

答案 1 :(得分:1)

AFAIK你不能改变默认后退按钮本身的动作,但你可以将UIBarButtonItem作为leftBarButtonItem放在那里并分配你自己的动作。

如果定义了leftBarButtonItem,则会显示此内容,而不是默认的后退按钮。

但是,在做这样的技巧时,请牢记GUI指南。

答案 2 :(得分:0)

UINavigationBar中的后退按钮会在您推送新的UIView时自动生成。为了让你自定义后退按钮是创建一个新的UIToolBar +一个带有自定义视图的UIBarButtonItem。

以下代码是在UIToolBar中使用自定义UIBarButtonItem的示例。

    // create button
UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape!
[backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:@"Back" forState:UIControlStateNormal];

// create button item -- possible because UIButton subclasses UIView!
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

// add to toolbar, or to a navbar (you should only have one of these!)
[toolbar setItems:[NSArray arrayWithObject:backItem]];
navItem.leftBarButtonItem = backItem;

下面的链接是PSD格式的iOS按钮设计,以便进一步修改。

http://www.chrisandtennille.com/pictures/backbutton.psd

答案 3 :(得分:0)

您可以制作自定义按钮并对其进行操作,但您无法更改默认的backButton操作.....

self.navigationItem.leftBarButtonItem = getBackBtn;

答案 4 :(得分:0)

UINavigationController在推送和弹出ViewController时向其委托发送消息。

您可以通过实施以下内容并在.h文件中添加<UINavigationControllerDelegate>来了解何时按下后退按钮

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
}

-(void)navigationController:(UINavigationController *)navigationController
     willShowViewController:(UIViewController *)viewController 
                   animated:(BOOL)animated{

    //Test here if the View Controller being shown next is right below the current
    //    ViewController in the navigation stack
    //
    //Test by:
    // 1. comparing classes, or
    // 2. checking for a unique tag that you previously assigned, or
    // 3. comparing against the [navigationController viewControllers][n-2] 
    //        where n is the number of items in the array

    if ([viewController isKindOfClass:NSClassFromString(@"ViewControllerClassThatGetsPushedOnBACK")){
        //back button has been pressed
    }

    if (viewController.tag == myUniqueTagIdentifier){
        //back button has been pressed
    }

    if ([navigationController.viewControllers[navigationController.viewControllers.count-2]==viewController]){
        //back button has been pressed
    }
}

Apple Docs UINavigationController Class Reference:

  

根视图控制器位于数组中的索引0处,即后视图   controller位于索引n-2,顶层控制器位于索引n-1,   其中n是数组中的项目数。