iOS在两个控制器之间切换

时间:2012-04-28 18:28:40

标签: iphone objective-c ios xcode cocoa-touch

我的委托上有两个制表符控制器,我想根据需要切换它们。 RootController默认显示,当我想从我使用的另一个类显示secondController时,

    myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.window addSubview:appDelegate.secondController.view];
    [appDelegate.rootController.view removeFromSuperview];

问题是当我尝试使用相同的方法返回时。因访问异常而导致崩溃...

  myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];   
    [appDelegate.window addSubview:appDelegate.rootController.view];
    [appDelegate.secondController.view removeFromSuperview];  

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:2)

您不应该从Superview中删除rootController。删除该行

[appDelegate.rootController.view removeFromSuperview]; 

当你想回到rootController时,只需添加以下行:

myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.secondController.view removeFromSuperview];