如何以编程方式移动到另一个UIViewController?

时间:2012-09-04 23:39:37

标签: objective-c ios uiviewcontroller

我目前正在CurrentViewController中执行一个方法,当我到达方法的末尾时,我想移动到我的FirstViewController(这是我要移动到的ViewController的实际名称)目前正在执行

CurrentViewController.m

-(void)methodExecutingOnCurrentViewController
{
    //some code..

    // what method do I call in order to load and move to my FirstViewController at this point?

    // do I first initialize an instance of the ViewController I want to move to?
    UIViewController *firstViewController = [[FirstViewController alloc] init];


    // but now how do I actually move to this instance I've just created? 
    // I couldn't find the appropriate method in the class reference documentation

}

4 个答案:

答案 0 :(得分:1)

查看- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated Doc

还有很多其他方法可以帮助您实现所需的目标,您使用的是导航控制器吗?那么- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated就是你想要的。您也可以将该控制器的视图作为子视图添加到当前的viewController。

答案 1 :(得分:1)

如果你正在使用UINavigationController(如果你不是,你应该),你可以这样做:

[self.navigationController.pushViewController pushViewController: firstViewController animated:YES];

这会将视图控制器推送到导航堆栈。

您还可以以模态方式呈现视图控制器。你应该阅读它here并决定什么是最适合你的。

答案 2 :(得分:1)

将您的项目基于导航控制器,只需按

即可
  

SecondViewController * secondVC = [[SecondViewController alloc] init];

     [self.navigationController secondVC animated:YES];

答案 3 :(得分:0)

如果你不使用UINavigationController,你可以通过这种方式移动到其他UIViewcontroller:

NewViewController *newVC = [[NewViewController alloc] init];
[self dismissViewControllerAnimated:YES completion:^{[self presentViewController:newVC animated:YES completion:^{}];}];