如何在iOS 8中取消两个UIViewControllers?

4

我正在使用Objective C开发iPhone应用程序。由于需要同时关闭两个UIViewControllers,因此我正在使用以下代码:

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

这段代码在iOS6和iOS7中正常工作,但在iOS8中却无法正常工作。我使用断点进行了检查,发现我的ViewController的viewDidLoad和viewWillAppear方法在调用,但是我的视图根本没有加载,因此输出的结果是一个空白的白屏。请问有人能够帮我解决这个问题吗?在iOS8中,我应该使用presentedViewController代替presentingViewController吗?


如果有多个VC正在呈现,我会向您想要解除显示的VC添加通知观察器,并发送通知询问其是否离开。 - Horst
感谢@Horst的回复。但根据我的要求,需要关闭两个ViewControllers。 - Anand Gautam
然后将观察者添加到两个视图控制器中。 - Horst
2个回答

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);
    });
}


示例项目


Objective-C 代码

- (IBAction)goBack:(id)sender {

    UIViewController *trmpVC = self.presentingViewController;

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


示例项目


@Jageen感谢您的回答,我正在使用Objective C,不太了解shift。如果可能,请给我提供Objective C的解决方案。 - Anand Gautam
@AnandGautam 已经编辑并将上传示例项目。 - Jageen
@AnandGautam 在你的环境中也做了工作吗?如果没有,请检查示例项目。 - Jageen
让我们在聊天中继续这个讨论 - Anand Gautam

1
尝试以下操作:
NSArray *arrVC = [self.navigationController viewControllers];
[self.navigationController popToViewController:[arrVC objectAtIndex:arrVC.count-2] animated:YES];

希望这有所帮助。

它具体是做什么的?你能详细说明一下吗? - Anon
没问题。你的代码崩溃是由于索引问题导致的。实际上,应该是NSInteger currentIndex = [self.navigationController.viewControllers indexOfObject:self]; 如果( currentIndex-2 >= 0 ) { [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:currentIndex-2] animated:YES]; } - Anand Gautam
不,不是这样的。实际上我已经尝试过了,但它根本没有被解除。 - Anand Gautam
我认为你应该采用委托方法,弹出视图控制器时发送消息给委托以再次弹出视图控制器。 - Anon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接