2012-09-04 28 views
1

我目前执行的CurrentViewController的方法,我想搬到我FirstViewController当我到达方法的当前正在执行如何以编程方式移动到另一个UIViewController?

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 

} 

回答

1

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animatedDoc看看。

还有很多其他的方式可以完成你正在寻找的东西,你使用的是导航控制器吗?然后- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated是你想要的。您也可以将该控制器的视图作为子视图添加到当前的viewController中。

+0

我不使用导航控制器(我得考虑这样做..),但会'presentViewController:动画:完成:'无论如何现在做这项工作还是有任何风险?谢谢! –

+0

你应该没问题。只要记住,你需要知道什么时候该新的viewController需要解雇,所以你可以调用' - (void)dismissModalViewControllerAnimated:(BOOL)animated'。每个文档“呈现视图控制器负责解除其呈现的视图控制器,但如果您在呈现的视图控制器本身上调用此方法,它会自动将该消息转发给呈现视图控制器。 – ohr

+0

如果你想做更复杂的事情,你应该给文档一个好看的样子,否则你会遇到问题。祝你好运! – ohr

1

如果您正在使用一个UINavigationController(如果你没有,你应该),你可以这样做:

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

这推动一个视图控制器到导航堆栈。

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

+0

当你是一个新手时,这是一个令人讨厌的事情,你的项目变得太大,你从来没有想过使用NavigationController,现在我只是太害怕向我的项目介绍这一切,并与所有必要的变化搞砸:) –

+1

它真的不那么难!你需要做的只是在你的'applicationDidFinishLaunching'中,而不是将你的视图控制器设置为根视图控制器,使用'UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController创建一个导航控制器: yourViewControllerHere]'并将其设置为根视图控制器。那么你很好走! – jere

+0

如何才能全局访问我创建的导航控制器?我的意思是每当我在一些xViewController中,我需要移动到一些yViewController我想我需要弹出该xViewController并通过调用我的navigationController将该yViewController推到我的堆栈上。我在哪里创建它,以及如何通过它访问它任何ViewController是什么使我困惑.. –

1

基地您在导航控制器项目和根本就推

SecondViewController * secondVC = [[SecondViewController的alloc] INIT];

 [self.navigationController secondVC animated:YES]; 
0

如果你不使用的UINavigationController,您可以通过这种方式转移到其他的UIViewController:

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