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,765
  • 45
  • 140
  • 253

3 Answers3

72

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
  • 15,601
  • 23
  • 93
  • 150
  • 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
  • Thanks Stu! Almost 10 yrs later, still working in Xcode 14. Modifying my app to add a horizontal scroll view with buttons to navigate to about 10 other view (getting rid of Next/Back buttons to move 1 screen at a time like a book). This is awesome. Users will be very happy. – user3741598 Jan 04 '23 at 23:35
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,362
  • 1
  • 17
  • 20
0

Completely w/o segues

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