27

I have two view controllers in my storyboard and I need to push from view 1 to view 2.

I need to do this without connecting a segue directly from a button in my storyboard, I need to do it programatically.

How can I?

Thanks.

Josh Kahane
  • 16,388
  • 44
  • 131
  • 248

3 Answers3

71

When you click the button call:

[self performSegueWithIdentifier:@"SegueIdentifier" sender:self];

You will need to create the segue in the storyboard but you can do it from view controller to view controller instead of button to viewcontroller.

StuStirling
  • 14,888
  • 22
  • 88
  • 143
  • This make the navigation so weird. It doesn't animate, also the destination view take too long time to layout. Any idea? – Envil Apr 08 '14 at 16:58
  • What is the difference between this and using the navigation stack? – Subby Jul 19 '14 at 22:24
  • 1
    @Subby you have to use Navigation controller for moving to next view either Segue or Navigation stack. But using Segue controller puts a visual wiring in your storyboard and you can visually see each screen source and destination. This is very helpful when you have 30 to 40 screens in your app – AsifHabib Nov 30 '15 at 09:20
0

If you want to perform an action without click (or while loading the viewController itself), do this code in either

- (void)viewDidLoad {
[super viewDidLoad];
[self performSegueWithIdentifier:@"NameOfYourSequeIdentifier" sender:self];
 }

or

-(void)viewDidAppear:(BOOL)animated {
[self performSegueWithIdentifier:@"NameOfYourSequeIdentifier" sender:self];
}
Ram Madhavan
  • 2,314
  • 1
  • 16
  • 20
0

Completely w/o segues

[self.navigationController pushViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerIDinStoryBoard"] animated:YES];
Boris Gafurov
  • 1,396
  • 15
  • 28