Above is my storyboard. The first view is hooked up to ViewController, and the second isn't connected to anything. I know how to show the second view controller if I had a button on the first. I can click and drag from the button to the second view, creating a segue. However, this time, I have to show the second view controller programmatically inside ViewController.m. For example, in when some function myFunc is called, I want to show the second view controller. Could someone explain how I can do this?
I think I need to know the followings:
- How to reference the second view controller in ViewController.m
- How to open the second view controller
Thank you!
UPDATE:
//setSelectedDate is myFunc. It lives inside myView.m, which is a subview of UIView
- (void)setSelectedDate:(NSDate *)newSelectedDate; {
NSLog(@"override worked");
[super setSelectedDate:newSelectedDate];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
UIViewController *firstController = [storyboard instantiateViewControllerWithIdentifier:@"InitialViewController"];
UIViewController *myController = [storyboard instantiateViewControllerWithIdentifier:@"MySecondViewController"];
[firstController presentViewController:myController animated:YES completion:nil];
}
I get the following warning if I do above:
Warning: Attempt to present on whose view is not in the window hierarchy!
and from this post I believe it's because I am doing it in the wrong place. How can I resolve this issue?
**Update 2: I did the following: **
- (void)setSelectedDate:(NSDate *)newSelectedDate; {
[super setSelectedDate:newSelectedDate];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
UIViewController *firstController = [storyboard instantiateViewControllerWithIdentifier:@"InitialViewController"];
[firstController performSegueWithIdentifier:@"GoToSecondViewController" sender:firstController];
The warning is gone, but the view controller is still not showing. Is it because I am calling this inside a View instead of a ViewController?