Is there a way to stop the output of a UIViewController from the UINavigationController stack when the backBarButtonItem inverse is used?

I have a UINavigationController with a root view controller, and then I push the UIViewController on the navigation controller stack. When the user drops the backBarButtonItem, I would like to be able to see a warning popup if there are certain conditions and cancels the view controller pop. For example, a user may make certain selections, but some combination of them may be invalid, so I want to notify them of changes.

I know that I can prevent the user from making an invalid combination or display a warning when the wrong combination is selected, but I would not. The user can change the selection and may know that a certain combination is invalid, but I would prefer that they choose what makes the combination invalid, and then change something else (and notify them if they have not made changes before trying to go to the previous screen). For example, if I forbid them to choose something that makes an invalid combination, then they may have to scroll the screen, change something, then scroll back instead of making choices, and then scroll and change something.

Using viewWillDisappear: does not work, because although I can create a warning view, I cannot determine how to prevent pop from appearing. A warning window is displayed, but the view controller still appears, and they return to the root view controller (with the warning view displayed).

Is there any way to prevent pop from appearing? If not, does it need something to stand in order to report an error or is it superfluous and / or esoteric?

+4
source share
3 answers

This sounds like something more suitable for a Modal View Controller than for a View controller in the navigation stack.

If you are married to do this on the stack, it would be nice if you could do it using the UINavigationControllerDelegate , but you cannot.

Can I turn off the back button until the values ​​are valid? Perhaps when the user tries to enter something, but it is invalid, at the top of the View window you have added a shortcut with red text that tells the user that they need to fix it. However, the back button is disabled, and it is back on after making corrections.

Or get a truly creative approach to how your user interface controls work to ensure that the user can never enter bad data.

+1
source

You can replace the back button with your own, which calls the method you want in loadView

 cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(onCancelButtonSelected:)]; self.navigationItem.leftBarButtonItem = cancelButton; 

and then

 -(void)onCancelButtonSelected:(id)sender { if (!hasSavedProduct) { cancelAlert = [[UIAlertView alloc] initWithTitle:@"Product not saved" message:@"Exit without saving?" delegate:self cancelButtonTitle:@"Exit" otherButtonTitles:@"Save", nil]; [cancelAlert show]; [cancelAlert release]; } 

then let them go

 - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if ([cancelAlert isEqual:actionSheet]) { if (buttonIndex == 0) { NSLog(@"Fine. Exiting without saving"); [self.navigationController popViewControllerAnimated:YES]; } else { NSLog(@"Save here"); } } 
+6
source

Let them come back, just do not save anything, if it is not fully valid. This is usually Apple's approach.

+1
source

Source: https://habr.com/ru/post/1307837/


All Articles