0

I have a UITableViewController that loads up a TabBarViewController on selecting a row.

The user can change information on the screen, and then hit the Back button in the Navigation controller. The TabBarViewController is of kind Show (e.g. Push) via a storyboard segue.

If the user changes something on the screen and then hits the back button, I want to be able to display an alert asking if they really want to leave this screen. OR if they switch tabs.

I used this SO question as a reference however I can't seem to get it to hook up right.

If you look at below screenshot you can see my tabs are broken up into various items.

enter image description here

The "Vehicle View Controller" is where I would like to monitor for changes using a bool didMakeChanges that gets set to true when edits are made.

In my viewDidLoad in the VehicleViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    didMakeChanges = false;

    //Here I am holding a reference to the TabViewContoller
    self.mvc = (MasterTabViewController*)self.tabBarController;


    UIBarButtonItem *bbtnBack = [[UIBarButtonItem alloc] initWithTitle:@"GO" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack:)];

    [self.navigationItem setBackBarButtonItem: bbtnBack];

}

- (void)goBack:(UIBarButtonItem *)sender
{
    if (didMakeChanges) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Changes Made"
                                                        message:@"Discard the vehicle changes without saving?"
                                                       delegate:self
                                              cancelButtonTitle:@"No"
                                              otherButtonTitles:@"Yes", nil];
        [alert show];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(buttonIndex) {
        case 0: //"No" pressed
            break;
        case 1: //"Yes" pressed
            [self.navigationController popViewControllerAnimated:YES];
            break;
    }
}
Community
  • 1
  • 1
user-44651
  • 3,924
  • 6
  • 41
  • 87
  • At the moment what fails? Does it not push the tab bar, does the alert view not appear when you click back? Also does each one of your tabs have a navigation bar? – JingJingTao Jul 15 '16 at 22:35
  • The alertview does not fire. I have included the delegate. All tabs share the same Nav controller. The tab VC is embedded in a navigation controller – user-44651 Jul 15 '16 at 23:53
  • Okay, a break point triggers if you set one in the goBack method, obvious I know, but worth checking. Have you tried using UIAlertController instead of UIAlertView? – JingJingTao Jul 16 '16 at 00:17

0 Answers0