Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PresentModalViewController in Storyboard programmatically iOS 5

I am using storyboards for FIRST time in my iOS app. I have 2 views in my Storyboard (A & B). Lets say A is my initial view controller in my storyboard. When my app launched, I can see view controller A. So far everything is working as per expectation. Now in my view controller A, I am checking whether user is logged in or not. If user is not logged in then I want to present view controller B. How can I show B modally using PresentModalViewController programmatically?

Here is my set up

enter image description here

Here is my code

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    if (!isUserLoggedIn) {
        NSLog(@"USER NOT LOGGED IN....");
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        LoginViewController *vc = (LoginViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"B"];
        [self presentModalViewController:vc animated:YES];
    }
}
like image 1000
iOSAppDev Avatar asked Jul 26 '12 09:07

iOSAppDev


1 Answers

What you have done so far seems correct.. Did you remember to actually set the identifier of B in the storyboard?

Also, you might want to try

[self.storyboard instantiateViewControllerWithIdentifier:@"B"];

instead of what you're doing.

Update:

Here's what the viewDidLoad method might look like:

- (void)viewDidLoad {

    [super viewDidLoad];

    if (!isUserLoggedIn) {

        NSLog(@"User is not logged in.");

        LoginViewController *vc = (LoginViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"B"];
        [self presentModalViewController:vc animated:YES];

    }

}

Also, I see from the image that your first view controller isn't set to any particular class. It just says "View Controller", while the second one shows "Login View Controller" correctly.

Note: I don't have access to Xcode right now, so I haven't tested it yet.

like image 176
matsr Avatar answered Sep 20 '22 17:09

matsr