【问题标题】:“Application tried to present modally an active controller” Error in iOS5“应用程序试图以模态方式呈现活动控制器”iOS5 中的错误
【发布时间】:2011-12-16 17:35:08
【问题描述】:

我有一个错误导致我的应用在 iOS5 下仅在 iPad 上崩溃。

当用户点击 uibarbutton 项目中的项目时,将调用以下代码:

- (void)optionSelected:(NSString *)option {

[self.optionPickerPopover dismissPopoverAnimated:YES];

if ([option compare:@"Map View"] == NSOrderedSame) {
    NSLog(@"Map View"); 
    MapView * map = [[MapView alloc] initWithNibName:@"MapView" bundle:nil]; 

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:map];

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                    style:UIBarButtonItemStyleDone target:self action:@selector(removeCurrent)];
    map.navigationItem.rightBarButtonItem = rightButton;

    [self presentModalViewController:navigationController animated:YES];

    [navigationController release];
    [map release];     
    [rightButton release];
    [split presentModalViewController:map animated:YES];
}

谁能说明为什么会在 iOS5 中发生这种情况?

【问题讨论】:

    标签: iphone xcode ipad sdk ios5


    【解决方案1】:

    您收到此错误是因为您尝试显示“地图”视图控制器两次。第一次是作为'navigationController'的根视图控制器,第二次是通过[split presentModalViewController:map animated:YES]。

    当你尝试用视图控制器做一些奇怪的事情时,iOS 5 比 iOS 4 更挑剔一些。尝试两次显示同一个控制器是一个设计问题——你需要弄清楚你真正想要做什么并修复它。

    (另外,调用地图视图控制器“MapView”而不是“MapViewController”确实令人困惑)

    【讨论】:

      【解决方案2】:

      如果您不遵循以下准则,也会出现此错误:Creating Custom Content View Controllers

      基本上,你需要调用:

      [你的VC removeFromParentViewController];

      如果你有

      [parentVC addChildViewController:yourVC];

      这个错误可能经常与“UIViewControllerHierarchyInconsistency”有关

      【讨论】:

        猜你喜欢
        • 2018-08-24
        • 2013-01-06
        • 2011-12-25
        • 1970-01-01
        • 1970-01-01
        • 2014-03-03
        • 2013-02-15
        • 1970-01-01
        • 2017-09-02
        相关资源
        最近更新 更多