IPhone - change the target or selector for the back button to a UINavigationController

The default behavior when clicking the UIViewController on the UINavigationController means that the OS displays a return button, which again removes the UIViewController.

I have a desire to set a different behavior for this return button (to return to two screens) - anyway, I can do this without creating my own return button with custom graphics, etc.

Thanks:)

+2
source share
5 answers

As I suspected at the beginning, this is impossible without much work. In the same way it is used when creating any custom UIBarButtonItem, you just need to specify 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]; 
+4
source

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

+2
source

Is there something wrong with the UIViewController navigationItem? Here, how can I get the cancel button, for example:

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

In the parent view controller

 - (void)viewDidLoad { self.navigationController.delegate= self; } - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { if (viewController == self) { // your codes } } 
0
source

If you subclass your navigation controller, you can implement the popViewControllerAnimated: method and throw out isKindOfClass: check there to see if the view controller you are looking for is displayed. For instance:

 - (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 sliding of the view controller (returning nil will cause the controller to fail, but it will still cause the navigation panel to display its information and return NO to the shouldPop: delegate method in the navigation panel. Still expose the controller independently. I heard that this only happens when using the navigation controller, but I have not tested this).

For your situation, however, since you want to open the two view controllers again, you could remove the second last view controller from the navigation controller's view controller property, converting the view controllers to nsmutablearray, removing the controller and then converting that nsmutablearray back to the array and setting it as Property for controlling the viewing of the navigation controller. I have not tested this, but I thought I would share this idea.

0
source

All Articles