开发者

Block a navigation controller from navigating and ask for user confirmation

开发者_开发技巧I have a UITableViewController that uses didSelectRowAtIndexPath: to init a view controller with initWithNibName:. The user is able to return to the table view but I would like to perform a if/else and display an alert view to confirm if the user would like to return to the table view. I have no clues where to start.

In short, I would like to customize the back button.


Can't you create a UIBarButtonItem instance and assign it to the view controller's navigation item? Something like this,

UIBarButtonItem * backButton = [[[UIBarButtonItem alloc] initWithTitle:@"Back" 
                                                                 style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(backButtonPressed:)] autorelease];
self.navigationItem.leftBarButtonItem = backButton;

and then in backButtonPressed:, trigger an alert view.

- (void)backButtonPressed:(id)sender {
    // Trigger an alert view.
}

Based on the user choice, you can pop the view controller or not.


The button that returns the user is an IBAction (or it's a method that you define when you set the button up programmatically), so you can add the code from here into the IBAction and put the code to return in

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜