72

I have looked at all the tutorials I can find on this one, and I still don't have the answer. I need to call another view from the code. I am using UIStoryboards. I have changed the view many times by control-dragging from UIButtons, but now it must be from the code. I am trying to call the info page from the main menu if it is the first time the user has opened the app. I cannot seem to find a way to change the views from the code, however. All my views are controlled by the same files (ViewController2). The identifier of my main menu is ViewControllerMain, and the identifier of the info page is ViewControllerInfo. First I tried this:

[ViewControllerMain presentViewController: ViewControllerInfo 
                                 animated:YES 
                               completion: NULL];

Then I tried making different UIViewControllers for each and saying:

[ViewController2 presentViewController: ViewController 
                              animated:YES 
                            completion: NULL];

Neither worked. For the first one, it says:

Use of undeclared identifier ViewControllerMain.

In the second one, it says:

unexpected interface name 'ViewController': expected identifier.

What can I do?

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
John Farkerson
  • 2,543
  • 2
  • 23
  • 33

8 Answers8

137

To create a view controller:

UIViewController * vc = [[UIViewController alloc] init];

To call a view controller (must be called from within another viewcontroller):

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

For one, use nil rather than null.


Loading a view controller from the storyboard:

NSString * storyboardName = @"MainStoryboard"; 
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];
[self presentViewController:vc animated:YES completion:nil];

Identifier of your view controller is either equal to the class name of your view controller, or a Storyboard ID that you can assign in the identity inspector of your storyboard.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
190290000 Ruble Man
  • 2,173
  • 1
  • 20
  • 25
  • 2
    Hi @190290000 Ruble Man. This works, but I would like to show an already created view from the storyboard file. This just gives me a blank view. Is there a way to do this? I am doing my best. I definitely have a lot to learn. – John Farkerson Apr 21 '13 at 19:53
  • Say, if line code UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"]; execute multiple times then new *vc object created every time? what about earlier *vc object ? is this memory leak? – Avijit Nagare Aug 30 '16 at 11:08
  • @AvijitNagare Nah man, thanks to ARC (assuming you're using it) the reference will get released automatically, so you don't have to worry about it. If you're saying to create multiple variables with the same name, that will cause a compiler error so it won't even let you do that. – John Farkerson Sep 21 '16 at 17:47
  • ok. means UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR‌​_VIEWCONTROLLER"]; creates new variable each time. but ARC manage its memory. – Avijit Nagare Sep 21 '16 at 18:24
21

You need to instantiate the view controller from the storyboard and then show it:

ViewControllerInfo* infoController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerInfo"];
[self.navigationController pushViewController:infoController animated:YES];

This example assumes that you have a navigation controller in order to return to the previous view. You can of course also use presentViewController:animated:completion:. The main point is to have your storyboard instantiate your target view controller using the target view controller's ID.

tigloo
  • 644
  • 3
  • 8
20

Swift

This gets a view controller from the storyboard and presents it.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "secondViewControllerId") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)

Change the storyboard name, view controller name, and view controller id as appropriate.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • what if i dont have any view controller in storeyboard but all is i am creating a programatic class how can i create a view controller programatically and assign it a class and present it – iOS Developer Aug 28 '18 at 04:56
  • @iOSDeveloper, I don't know. – Suragch Aug 28 '18 at 05:20
5

You can call ViewController this way, If you want with NavigationController

enter image description here

1.In current Screen : Load new screen

VerifyExpViewController *addProjectViewController = [[VerifyExpViewController alloc] init];
[self.navigationController pushViewController:addProjectViewController animated:YES];

2.1 In Loaded View : add below in .h file

@interface VerifyExpViewController : UIViewController <UINavigationControllerDelegate>

2.2 In Loaded View : add below in .m file

  @implementation VerifyExpViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationController.delegate = self;
    [self setNavigationBar];
}
-(void)setNavigationBar
{
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"B_topbar.png"] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Btn_topback.png"] style:UIBarButtonItemStylePlain target:self action:@selector(onBackButtonTap:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor lightGrayColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Save.png"] style:UIBarButtonItemStylePlain target:self action:@selector(onSaveButtonTap:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor lightGrayColor];
}

-(void)onBackButtonTap:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)onSaveButtonTap:(id)sender
{
    //todo for save button
}

@end

Hope this will be useful for someone there :)

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
3

There's 2 ways you can do this:

1, Create a segue to your ViewController in your Storyboard as explained in my answer here: How to perform a segue that is not related to user input in iOS 5?

2, Give your ViewController and identifier and call it using the code in my answer here: Call storyboard scene programmatically (without needing segue)?

Community
  • 1
  • 1
Darren
  • 10,182
  • 20
  • 95
  • 162
1

main logic behind this is_,

NSString * storyboardIdentifier = @"SecondStoryBoard";

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: nil];

UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:@"YourviewControllerIdentifer"];

[self presentViewController:UIVC animated:YES completion:nil];
Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68
Vaibhav Shiledar
  • 939
  • 8
  • 15
  • what if i dont have any view controller in storeyboard but all is i am creating a programatic class how can i create a view controller programatically and assign it a class and present it – iOS Developer Aug 28 '18 at 04:56
1
        UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone_iOS7" bundle:nil];
            AccountViewController * controller = [storyboard instantiateViewControllerWithIdentifier:@"accountView"];
            //            [self presentViewController:controller animated:YES completion:nil];

        UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        while (topRootViewController.presentedViewController)
        {
            topRootViewController = topRootViewController.presentedViewController;
        }

        [topRootViewController presentViewController:controller animated:YES completion:nil];
MBH
  • 16,271
  • 19
  • 99
  • 149
suresh
  • 31
  • 9
  • 3
    the question is 3 years old with an accepted answer, if you have a valuable contribution please explain it rather than just posting code. – pancho018 Nov 11 '16 at 10:27
0

Import the view controller class which you want to show and use the following code

KartViewController *viewKart = [[KartViewController alloc]initWithNibName:@"KartViewController" bundle:nil];
[self presentViewController:viewKart animated:YES completion:nil];
vijeesh
  • 1,317
  • 1
  • 17
  • 32