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.
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;
}
}