4

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.

Pasha Immortals
  • 829
  • 1
  • 7
  • 19

4 Answers4

8

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.

mackworth
  • 5,873
  • 2
  • 29
  • 49
  • 1
    I thought of disabling and enabling the back button but based on what is edited on the second view controller, I really want to show some descriptive alerts first. Thats why I want to some how cancel the going back. Instead of the user not knowing whats wrong and why the back button is enabled, or me tieing up alerts to the events of my text fields. – Pasha Immortals Aug 10 '12 at 05:37
  • 1
    Well, I think your only option then is to make your own back button (setLeftBarItem), and point it to your own routine. You can still pull its name from your parent view controller in the nav stack to have it named the same as before. – mackworth Aug 10 '12 at 05:48
  • thanks @macworth obviously you can tell I'm new to this and was really hoping there would be a view event for kind of unloading that I could cancel like the other types of events Ive seen where the return type is BOOL, so could have easily can'd it by returning NO. – Pasha Immortals Aug 10 '12 at 06:00
1

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];
}
Rémy Blanc
  • 313
  • 2
  • 9
0

sorry,i'm not good at english,so reading so long text is a little difficult for me.i can only get that you want to go back without a button. i known navigationController has a method to go back to previous view.

Wenhuang Lin
  • 214
  • 2
  • 8
  • No not correct @Lin, I want to stop the default back button if I wanted to without creating my own back button. – Pasha Immortals Aug 10 '12 at 05:39
  • you want to change the behavior of the default back button?i have a way:set a button can't be seen upon the default button,it does what you want't,then enable it if you don't want to use the default button,disable it if you wan't go back as default. – Wenhuang Lin Aug 10 '12 at 05:53
  • Lin, I don't think what I want to do is possible according to @mackworth's comments, so I'll use a custom button. – Pasha Immortals Aug 10 '12 at 05:59
  • i didn't notice i said the same thing with "mackworth".sorry. – Wenhuang Lin Aug 10 '12 at 06:05
  • i should remind you that when you use a custom button,you can only get a rectangle shape. – Wenhuang Lin Aug 10 '12 at 06:07
  • Mate you can create the right shaped custom button you want it just takes more code, here is where I got an idea of how to do it the day I asked this question:@AndrewS answer here gave me the basic idea of it mate. Here is the link: http://stackoverflow.com/questions/227078/creating-a-left-arrow-button-like-uinavigationbars-back-style-on-a-uitoolba – Pasha Immortals Aug 12 '12 at 23:20
0

Make a custom back button and use self.navigationItem.hidesBackButton=YES Add target to your custom back button and use it as you like. When you want to move the view you can use [self.navigationController popViewcontroller].

Dushyant Singh
  • 721
  • 4
  • 13