开发者

How to find out if the back-button was tapped in the UINavigationBar?

Firstly I have tried to manage it with the viewWillDisapper-method, but this not detailed enough for me. Is their an other solution?

Also tried the delegate:

开发者_如何学Python- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item

but nothing happens.


You need to change the default back button, in viewDidLoad :

- (void) viewDidLoad
{
   self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back"
                                           style:UIBarButtonItemStyleBordered
                                           target:self
                                           action:@selector(handleBack:)] autorelease];
}

And of course you have to pop the controller in your method :

- (void) handleBack:(id)sender
{
    // ... your code !

    [self.navigationController popViewControllerAnimated:YES];
}


You should try the other UINavigationBarDelegate delegate method, –navigationBar:shouldPopItem:, and return YES after doing whatever you need to do. "Should" delegate methods are called before the thing happens. "Did" methods are called after it happens.

The method that you are calling is not for the back button. The back button will "Pop" a view controller. The opposite (which you are using) is to "Push" a view controller. A push adds a new view controller to the stack. A pop removes a view controller from the stack.

Also, make sure to conform to UINavigationBarDelegate. If nothing happened with the delegate method that you used, something is set up wrong. AFAIK the delegate should be automatically set up if you are using a UINavigationController.


"Also tried the delegate: ... but nothing happens." First thing to do is to set a breakpoint inside the function which you suspect isn't called. To set up a breakpoint just click to the left of the code in xcode.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜