iPhone - Change target or selector for Back Button on UINavigationController

烈酒焚心 提交于 2019-12-21 06:18:36

问题


The default behaviour when pushing a UIViewController on a UINavigationController is for the OS to display a back button that pops the UIViewController off again.

I have the desire to set a different behavior for this back button (to go back two screens) - is there anyway I can do this without having to create my own back button with custom graphic etc.

Thanks :)


回答1:


As I half suspected originally, this isn't possible any exceptionally easy way. So same method applies when creating any custom UIBarButtonItem, just have to source the back button icon from Google....

UIButton *backButtonInternal = [[UIButton alloc] initWithFrame:CGRectMake(0,0,54,30)];
[backButtonInternal setBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal];
boldSystemFontOfSize:12]];
[backButtonInternal addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonInternal];   
[backButtonInternal release];
[[self navigationItem] setLeftBarButtonItem:backBarButton];
[backBarButton release];



回答2:


Using the "leftBarButtonItem" allows you to set the target and selector. But if you set the "backBarButtonItem" on the previous controller, the target and selector will be ignored. However, the leftBarButtonItem does not have the left pointing arrow.




回答3:


Anything wrong with UIViewController's navigationItem property? Here's how I get a cancel button, for example:

self.navigationItem.leftBarButtonItem =
  [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel
                                               target: self
                                               action: @selector(cancel)] autorelease];  



回答4:


In parent's viewcontroller,

- (void)viewDidLoad
{    
    self.navigationController.delegate= self;
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController == self)
    {
        // your codes
    }
}



回答5:


If you subclass your navigation controller, you can implement the popViewControllerAnimated: method, and throw an isKindOfClass: check in there to determine if the view controller you're looking for is being popped. Eg:

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    //Reference current controller being displayed
    UIViewController *currentController = [self.viewControllers lastObject];

    //Check class
    if ([currentController isKindOfClass:[MyDesiredController class]]) {
        NSLog(@"Popping Desired Controller, Do Stuff Here");
    }

    return [super popViewControllerAnimated:animated];
}

However this does not cancel the actual popping of the view controller (returning nil will stop the controller from popping but will still cause the navigation bar to pop it's information, and returning NO to the shouldPop: delegate method of the navigation bar will still pop the controller regardless. I have heard that this only occurs when using a Navigation Controller, but I haven't tested this).

For your situation however, since you desire to pop two view controllers back, you could possibly remove the second last view controller from the navigation controller's viewcontrollers property by converting the viewcontrollers to an nsmutablearray, removing the controller, and then converting this nsmutablearray back to an array and setting it as the navigation controller's viewcontrollers property. I haven't tested this but I thought I would share the idea.



来源:https://stackoverflow.com/questions/1519046/iphone-change-target-or-selector-for-back-button-on-uinavigationcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!