开发者

how to determine that the navigation bar's back button has been clicked and then showing a alert

working on a game based project.i am having a view controller as GameScreen. on the top of Gamescreen having a navigation bar with back button(default).Now if user cli开发者_如何转开发cks on back button i have to show alert. so how to determine "backbutton got clicked."? any suggestion? Thanks


In your viewdidload method :

UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"back_button.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

In the cancel method:

- (IBAction) cancel:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];

    or show your alert view
}

Otherwise also you can just override the method if you do not want a custom back button.


Your UINavigationBarDelegate has got a nice method for that, called shouldPopItem.

You can override that in your delegate and show there the alert. This gives you also a chance to cancel the going-back (popping).


Stopping the self.navigationItem.leftBarButtonItem from exiting a view - here you go maddy, answers the question in a different context but same basis -

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜