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
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];
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With