iOS: present view controller programmaticallly

2019-01-16 10:30发布

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.

9条回答
爷的心禁止访问
2楼-- · 2019-01-16 10:58

You need to set storyboard Id from storyboard identity inspector

 AddTaskViewController *add=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboard_id"];
 [self presentViewController:add animated:YES completion:nil];
查看更多
别忘想泡老子
3楼-- · 2019-01-16 10:59

The best way is

AddTaskViewController * add = [self.storyboard instantiateViewControllerWithIdentifier:@"addID"];
[self presentViewController:add animated:YES completion:nil];
查看更多
Evening l夕情丶
4楼-- · 2019-01-16 11:01

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];
查看更多
登录 后发表回答