Kite Runner Kite Runner - 1 year ago 55
iOS Question

How to push from view controller to navigation view controller using prepareForSegue method?

I have a project which has a view controller as initial screen and then a view controller embedded inside a navigational view controller. I also have a button on first screen on click of which I want the navigational controller screen to be opened.

I clicked on button and then on ' connections inspector', I added push event to that navigational controller, but segue is not happening. How could I achieve it please?

SOLUTION



Finally after a bit of research I managed to get this thing working. Here is the code i am using:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"Source Controller = %@", [segue sourceViewController]);
NSLog(@"Destination Controller = %@", [segue destinationViewController]);
NSLog(@"Segue Identifier = %@", [segue identifier]);

if ([segue.identifier isEqualToString:@"mysegue"])
{
NSLog(@"coming here");

SecondViewController *loginViewController = (SecondViewController *)segue.destinationViewController;

//SecondViewController *navigationController = [[UINavigationController alloc]init];

[self presentModalViewController:loginViewController animated:YES];


}

}

Answer Source

Are you sure that the Navigation Controller's connection to the embedded view controller is set up correctly? The first view controller should be connected to the Navigation Controller, with a Push style segue, and the navigation controller should be connected to the second view controller, with a Relationship style segue.

At any rate, one of the official Apple tutorials does just this, so you might be able to compare your code to it and see if there's a difference: Your Second iOS App. The prepareForSegue method itself isn't really involved with firing the segue; it is just invoked before the segue runs to prepare the new view controller.