iOS: present view controller programmatically

126,101

Solution 1

If you're using a storyboard, you probably shouldn't be using alloc and init to create a new view controller. Instead, look at your storyboard and find the segue that you want to perform; it should have a unique identifier (and if not, you can set one in the right sidebar).

Once you've found the identifier for that segue, send your current view controller a -performSegueWithIdentifier:sender message:

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

This will cause the storyboard to instantiate an AddTaskViewController and present it in the way that you've defined for that segue.


If, on the other hand, you're not using a storyboard at all, then you need to give your AddTaskViewController some kind of user interface. The most common way of doing so is to initialize the controller with a nib: instead of just calling init, you'll call -initWithNibName:bundle: and provide the name of a .xib file that contains your add-task UI:

AddTaskViewController *add = [[AddTaskViewController alloc]
                              initWithNibName:@"AddTaskView" bundle:nil];
[self presentViewController:add animated:YES completion:nil];

(There are other (less common) ways of getting a view associated with your new view controller, but this will probably present you the least trouble to get working.)

Solution 2

If you are using Storyboard and your "add" viewController is in storyboard then set an identifier for your "add" viewcontroller in settings so you can do something like this:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"NameOfYourStoryBoard" 
                                                     bundle:nil];
AddTaskViewController *add = 
           [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentifier"];

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

if you do not have your "add" viewController in storyboard or a nib file and want to create the whole thing programmaticaly then appDocs says:

If you cannot define your views in a storyboard or a nib file, override the loadView method to manually instantiate a view hierarchy and assign it to the view property.

Solution 3

You need to set storyboard Id from storyboard identity inspector

 AddTaskViewController *add=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboard_id"];
 [self presentViewController:add animated:YES completion:nil];

Solution 4

your code :

 AddTaskViewController *add = [[AddTaskViewController alloc] init];
 [self presentViewController:add animated:YES completion:nil];

this code can goes to the other controller , but you get a new viewController , not the controller of your storyboard, you can do like this :

AddTaskViewController *add = [self.storyboard instantiateViewControllerWithIdentifier:@"YourStoryboardID"];
[self presentViewController:add animated:YES completion:nil];

Solution 5

The best way is

AddTaskViewController * add = [self.storyboard instantiateViewControllerWithIdentifier:@"addID"];
[self presentViewController:add animated:YES completion:nil];
Share:
126,101
Salieh
Author by

Salieh

Updated on June 26, 2020

Comments

  • Salieh
    Salieh almost 4 years

    I'm using the presentViewController:animated:completion: method to go to another view controller.

    This is my code:

    AddTaskViewController *add = [[AddTaskViewController alloc] init];
    [self presentViewController:add animated:YES completion:nil];
    

    This code goes to the other UIViewController but the other controller is empty. I've always been using storyboards but now I need this to be done in code.

  • Salieh
    Salieh about 11 years
    If it is combined with a button its work. but is not combined.
  • Frederic Adda
    Frederic Adda about 8 years
    This just loads a blank UIViewController
  • Esqarrouth
    Esqarrouth about 8 years
    of course it does. You have to replace it with your own viewcontroller, or add some sort of color or objects inside the view of uiviewcontroller
  • Wai Ha Lee
    Wai Ha Lee about 8 years
    To reviewers: if the answer is bad but is an answer (like this is), don't recommend deletion: downvote! See You're doing it wrong: A plea for sanity in the Low Quality Posts queue. This is an answer. You may not agree with it, but it is an attempt to answer the question.
  • C. Tewalt
    C. Tewalt over 7 years
    So how do you "un-present" the new view controller in the non-storyboard scenario? I'm googling around trying to find something similar to unwindForSegue:towardsViewController:
  • C. Tewalt
    C. Tewalt over 7 years
    Actually, I just found "dismissViewController" which I think is the right answer.
  • Madhurya Gandi
    Madhurya Gandi over 5 years
    Thanks. Worked for me.