Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I switch views programmatically in a view controller? (Xcode, iPhone)

Have been struggling with this for a while, and can never seem to get a direct answer.

Any help is appreciated!

like image 508
mishajw126 Avatar asked Jun 07 '12 14:06

mishajw126


People also ask

How do I switch between views in Xcode?

The easiest way to switch between screens in iOS is. Add a button on the current screen. Then press control and drag the button to the target screen.

How do I change initial view controller programmatically?

Open mainstoryboard, select the view that you want start first, then open Utilities--> Attributes. Below the "View Controller" you see the "Is initial View Controller" radio button. Just select it.


2 Answers

If you're in a Navigation Controller:

ViewController *viewController = [[ViewController alloc] init]; [self.navigationController pushViewController:viewController animated:YES]; 

or if you just want to present a new view:

ViewController *viewController = [[ViewController alloc] init];     [self presentViewController:viewController animated:YES completion:nil]; 
like image 173
SlateEntropy Avatar answered Sep 21 '22 12:09

SlateEntropy


If you want to present a new view in the same storyboard,

In CurrentViewController.m,

#import "YourViewController.h"  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; YourViewController *viewController = (YourViewController *)[storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerIdentifier"]; [self presentViewController:viewController animated:YES completion:nil]; 

To set identifier to a view controller, Open MainStoryBoard.storyboard. Select YourViewController View-> Utilities -> ShowIdentityInspector. There you can specify the identifier.

like image 42
sree_achu Avatar answered Sep 22 '22 12:09

sree_achu