2012-09-29 231 views
0

我有一个主视图控制器并呈现子视图。对于儿童内部的其中一个设置,我需要再次调用主视图控制器。它会让事情变得更容易。从子视图控制器呈现主视图控制器

以下原因层次的问题:

#define appDelegate (MyAppDelegate *)[[UIApplication sharedApplication] delegate] 

[self presentViewController:[appDelegate mainViewController] animated:YES completion:^{}]; 

有什么办法可以轻松地调用mainViewController而不失子视图的状态?

编辑

这里是我如何呈现子视图。

ChildViewController *childViewController = [[ChildViewController alloc] init]; 
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:childViewController]; 
[childViewController release]; 
[self presentModalViewController:navigationController animated:NO]; 
[navigationController release]; 
+1

难道你不能只是创建一个新的父视图,并将其推送给孩子? – Kassem

+1

@KassemBagher是的,这是一个选项,这将是非常非常不方便的,因为有太多的初始化需要发生 – Dex

回答

2

您不能出示已经被提出或者是一个视图控制器在导航堆栈中。什么是mainViewController?你有没有实例化它?它已经添加到屏幕了吗?

如果对两者都是,你需要回到它(解雇它)或从它的父母先移除它,然后提出它。

+0

两者的答案都是肯定的。我已经更新了我的问题,以显示我是如何给孩子打电话的。什么是解决我的问题最简单的方法? – Dex

+0

你用mainViewController做什么? – Vinnie

+0

好的,我最终做的是将一个属性设置为'(nonatomic,retain)'的子视图的实例保存,然后将其解除以返回父视图。 – Dex

0

在你的孩子看,你应该能够访问你的应用程序委托其mainViewController财产,像这样:

MyApplicationDelegate *appDelegate = (MyApplicationDelegate *)[[UIApplication sharedApplication] delegate]; 
+0

是的,我试过这个,但是在'presentViewController'行得到一个错误。 – Dex

+0

什么是错误? –

+0

EXC_BAD_ACCESS(code = 2,address = ....) – Dex