11

How can you push to another view controller without prepareForSegue?

myClassVC *viewController = [myClassVC alloc];
UIStoryboardSegue *segue = [[UIStoryboardSegue alloc] initWithIdentifier:@"pushToMyVC" source:self destination:viewController];

if ([segue.identifier isEqualToString:@"pushToMyVC"]) {
 NSLog(@"logging");
 myClassVC *viewController = (myClassVC *)[segue destinationViewController];
 [self presentViewController:viewController animated:YES completion:nil];        
}
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
Marius Behrens
  • 159
  • 1
  • 2
  • 9

4 Answers4

46

If you want to programmatically invoke a push segue, you give the segue a "storyboard id" in Interface Builder and then you can:

[self performSegueWithIdentifier:"pushToMyVC" sender:self];

Alternatively, if you don't want to perform the segue, you can instantiate the destination view controller and then manually push to that view controller. All you need to do is to make sure that the destination view controller has its own "storyboard id" in Interface Builder, then you can:

UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationController"];
[self.navigationController pushViewController:controller animated:YES];

You said "push" (and hence I used pushViewController above). If you really meant to "present a modal view controller", then that second line is:

[self presentViewController:controller animated:YES completion:nil];

As you can see, you don't have to use prepareForSegue to push to new scene. You only use prepareForSegue if you want to pass information to the destination view controller. Otherwise it is not needed.

Clearly, if you're not using storyboards (e.g., you're using NIBs), then the process is entirely different. But I assume you're not using NIBs because prepareForSegue is not applicable in that environment. But if you were using NIB, it would be as follows:

SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
3

[self presentViewController:viewController animated:YES completion:nil]; is not needed, as the segue will push the destination view controller using the transition you selected automatically.

If you don't want to use the segue process you'll need to manually push the view controller with:

[self presentViewController:viewController animated:YES completion:nil];

but make sure you remove the segues in the Storyboard first.

Richard Brown
  • 11,346
  • 4
  • 32
  • 43
0
   NSString * storyboardName = @"Main";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewControllerID"];
    [self presentViewController:vc animated:YES completion:nil];
Naman Vaishnav
  • 1,069
  • 1
  • 13
  • 24
0

In my case all viewControllers are built dynamically. I do not make use of the storyboard for this purpose. Should this be your case you may just instantiate the viewController class you want to open and make a push as in my example below:

MyViewController* myViewController = [[MyViewController alloc] init];
[self.navigationController pushViewController:myViewController animated:YES];
Mig70
  • 61
  • 10
  • How can I prepare for segue in this case? – Konstantin Mar 30 '19 at 21:13
  • 1
    Easy! You just do it after the viewController instantiation and before you invoke the pushViewController method. You can set some public properties of the viewController, for instance. – Mig70 Mar 31 '19 at 22:12