【问题标题】:How to dismiss two UIViewControllers in iOS 8?如何在 iOS 8 中关闭两个 UIViewControllers?
【发布时间】:2014-12-15 10:16:20
【问题描述】:

我正在使用 Objective C 开发 iPhone 应用程序。因为我需要一次关闭两个 UIViewController,所以我使用以下代码:

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

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

【问题讨论】:

  • 如果有多个 vc 呈现,我会在你想解散的 VC 中添加一个通知观察者并发送通知要求它离开。
  • 感谢@Horst 的回复。但根据我的要求,需要关闭两个 ViewController。
  • 然后向两个 VC 添加观察者

标签: iphone ios8 presentmodalviewcontroller presentviewcontroller


【解决方案1】:

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


Objective-c 代码
- (IBAction)goBack:(id)sender {

    UIViewController *trmpVC = self.presentingViewController;

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


Sample project

【讨论】:

  • @JageenThansk 为您解答,我正在使用目标 c 对班次知之甚少。如果可能,请提供目标 C 中的解决方案。
  • @AnandGautam 已编辑并将上传示例项目。
  • @AnandGautam 在您的环境中也有效吗?,如果没有,请查看示例项目
【解决方案2】:

尝试以下操作:

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

希望这会有所帮助。

【讨论】:

  • 它到底是做什么的?你能详细说明一下吗?
  • 是的,确定.. 由于索引问题,您的代码正在崩溃。实际上应该是 NSInteger currentIndex = [self.navigationController.viewControllers indexOfObject:self]; if( currentIndex-2 >= 0 ) { [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:currentIndex-2] animated:YES]; }
  • no no.. 实际上我已经尝试过上述方法,但它甚至根本没有解雇。
  • 我认为你应该采用委托方法,弹出视图控制器发送消息以委托再次弹出视图控制器。
猜你喜欢
  • 1970-01-01
  • 2012-04-30
  • 1970-01-01
  • 1970-01-01
  • 2015-01-19
  • 1970-01-01
  • 2013-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多