Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stop view from disappearing

I am launching a viewController from another view controller via the push on its table views cell. Now on the second view controller I have a whole bunch of controls mainly test fields. I would like to by using the default back button provided in the second view controller so it'll be the title of the first view controller and keep in mind like I said default, so I don't want to create my own button for back on the second view controller. So would like to detect if the second view controller is exiting or disappearing or will disappear and based on certain conditions stop it from going back to the original caller view controller. I originally assumed it could be done in here:

-(void) viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) 
    {
        // So back button was pressed on the second view controller, so how do I stop it
        // here from going back to the original view controller. 
    }
}

Or how do I do it? I can't seem to find a view controller return type BOOL method to call and stop it.

Thanks.

like image 356
Pasha Immortals Avatar asked Jan 15 '23 20:01

Pasha Immortals


2 Answers

Note that method is called viewWillDisappear, not viewShouldDisappear, so Apple won't let you stop the view from disappearing at that point. And from the user's point of view, pressing the back button should indeed take them back. Instead you might consider hiding the back button under the circumstances where it's not allowed.

like image 174
mackworth Avatar answered Jan 24 '23 14:01

mackworth


You can't prevent the view controller from closing however you can emulate it.

This line will replace the close button with a button that will call a homemade function

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(closeBtn)];

So you will be able to close the view if you want:

- (void) closeBtn
{
    bool shouldClose=true;
    // Your conditions here
    if (shouldClose) [self dismissViewControllerAnimated:YES completion:nil];
}
like image 26
Rémy Blanc Avatar answered Jan 24 '23 14:01

Rémy Blanc