如何在iOS 8中关闭两个UIViewControllers?

时间:2014-12-15 10:16:20

标签: iphone ios8 presentmodalviewcontroller presentviewcontroller

我正在使用Objective C处理iPhone应用程序。因为我需要同时关闭两个UIViewControllers,所以我使用下面的代码:

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

此代码在iOS6和iOS7中运行良好,但在iOS8中无效。我已经使用断点检查,我的ViewController viewDidLoad和viewWillAppear方法正在调用,但我的视图根本没有加载,所以作为输出,我得到空白的白色屏幕。任何人都可以帮助我,对于iOS8,我该如何解决这个问题,我是否需要使用presentViewController而不是presentViewController?

2 个答案:

答案 0 :(得分:10)

Swift代码

@IBAction func goBack(sender: AnyObject) {
    var tmpController :UIViewController! = self.presentingViewController;

    self.dismissViewControllerAnimated(false, completion: {()->Void in
        println("done");
         tmpController.dismissViewControllerAnimated(false, completion: nil);
    });
}


Sample project

<小时/> 目标代码

- (IBAction)goBack:(id)sender {

    UIViewController *trmpVC = self.presentingViewController;

    [self dismissViewControllerAnimated:NO completion:^{
        [trmpVC dismissViewControllerAnimated:NO completion:nil];
    }];
}


Sample project

答案 1 :(得分:1)

请尝试以下操作:

NSArray *arrVC = [self.navigationController viewControllers];
[self.navigationController popToViewController:[arrVC objectAtIndex:arrVC.count-2] animated:YES];

希望这有帮助。