呈现控制器并使其成为根控制器

时间:2014-09-22 09:32:40

标签: ios objective-c xcode

我有一个方法,它提供一个控制器,并使控制器成为根控制器。它在Xcode 5中运行良好,但在Xcode 6中它崩溃了。我需要提供视图控制器并使其成为root,没有任何视图错误...旧代码崩溃,新的有错误,同时使它成为根控制器

原始代码

-(void)presentViewController:(UIViewController *)controller withCompletion:(void(^)(void))completion
{
    UIViewController *currentController;
    currentController = [self controller];
    [currentController presentViewController:controller animated:true completion:^{
        [controller dismissViewControllerAnimated:false completion:NULL];
        if(completion){
            completion();
        }
    }];
}

试图像延迟一样改变它

-(void)presentViewController:(UIViewController *)controller withCompletion:(void(^)(void))completion
{
    UIViewController *currentController;
    currentController = [self controller];
    [currentController presentViewController:controller animated:true completion:^{
        //use delay before execution
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [controller dismissViewControllerAnimated:false completion:NULL];
            if(completion){
                completion();
            }

        });
    }];
}

但是,我的视图在前一个视图中闪烁。也试图不在一个块

-(void)presentViewController:(UIViewController *)controller withCompletion:(void(^)(void))completion
{
    UIViewController *currentController;
    currentController = [self controller];
    [currentController presentViewController:controller animated:true completion:NULL];
    [controller dismissViewControllerAnimated:false completion:NULL];
    if(completion){
        completion();
    }
}

但这种方式我坚持以前的观点。

1 个答案:

答案 0 :(得分:0)

首先,您需要从Window对象获取主导航控制器:

UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
UINavigationController *rootNavController = (UINavigationController *)keyWindow.rootViewController;

并设置新的根视图控制器

UIViewController *rootViewController = [[UIViewController alloc] init];
[rootNavController setViewControllers:@[rootViewController] animated:NO];
[rootNavController pushViewController:rootViewController animated:YES completion:^{
    NSLog(@"rootViewController shown");
}];