SpokaneDude
SpokaneDude

Reputation: 4984

How to prevent user from leaving view controller?

I have an iPad app (XCode5, ARC, iOS7, Storyboards with a UITabBarController controlling the navigation). On one view, I have some required fields that I check for in -viewWillDisappear; if one of them is missing, I display an alert. The problem is I need to stay on that view until it's corrected. Unfortunately, the only place I can check for the required fields is in -viewWillDisappear.

Is there some way I can cause the view to complete the disappear and then go back to that same view? I have looked at SO and it doesn't appear to be a way, but I thought I'd ask anyway, just in case someone has figured out how to do it.. :D

Upvotes: 0

Views: 475

Answers (2)

micantox
micantox

Reputation: 5616

You need to do

self.tabBarController.delegate = self

in your viewdidload and then implement the delegate method

  - (BOOL)tabBarController:(UITabBarController *)tabBarController 
shouldSelectViewController:(UIViewController *)viewController
{
    if(conditions_satisfied)
        return YES;
    else
    {
        //show alert view here
        return NO;
    }
}

EDIT:It would appear that rdelmar was faster than me :)

Upvotes: 2

rdelmar
rdelmar

Reputation: 104082

You can set the delegate for the tab bar controller, and return NO from tabBarController:shouldSelectViewController: until whatever conditions you set are met.

Upvotes: 1

Related Questions