简体   繁体   English

如何关闭当前视图控制器和父视图控制器?

[英]How to dismiss current view controller and parentviewcontroller?

I am presenting two view controller like so我正在展示两个视图控制器

self.presentViewController(choosePlace, animated: true, completion: nil)
self.presentViewController(shareController, animated: true, completion: nil)

and I want to dismiss both of them like this:我想像这样解雇他们两个:

println("parent \(self.parentViewController)")
            self.dismissViewControllerAnimated(true, completion: {

            self.parentViewController?.dismissViewControllerAnimated(true, completion: nil)
            })

self.parentViewController is always nil though. self.parentViewController 始终为零。 How can I dismiss two at the same time?我怎样才能同时解雇两个?

You can:你可以:

self.dismissViewControllerAnimated(true, completion: { 
    self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
})

So, make sure that you are presenting your view controllers in order:因此,请确保按顺序呈现视图控制器:

parentViewController -> choosePlace -> shareController parentViewController -> choosePlace -> shareController

(arrows indicate "self.presentViewController") (箭头表示“self.presentViewController”)

Swift 4斯威夫特 4

There where some issue I faced while trying Michael Voline's answer.在那里我在尝试 Michael Voline 的回答时遇到了一些问题。

As the comment on the answer it did not work for me.作为对答案的评论,它对我不起作用。 it was because of the presentingViewController was nil.这是因为presentingViewController为零。 So we need to set in a different property say presentingController but this will not work if you are setting it on the viewDidLoad所以我们需要设置一个不同的属性,比如presentingController但是如果你在viewDidLoad上设置它,这将不起作用

private var presentingController: UIViewController?
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    presentingController = presentingViewController
}

then然后

    dismiss(animated: false, completion: {
         self.presentingController?.dismiss(animated: false)
    })

I made the animation false so that the user will not see the UIof presenting view controller in the split seconds of dismissing.我将动画设为 false,以便用户在关闭的几秒钟内看不到 UIof 呈现视图控制器。

You can reference the presenting view controller during the function 'viewWillDisappear' of your view controller (ie ShareController dismisses choosePlace just before it leaves scope).您可以在视图控制器的“viewWillDisappear”功能期间引用呈现视图控制器(即 ShareController 在它离开范围之前关闭选择位置)。

//place the below in shareController 
override func viewWillDisappear(_ animated: Bool) {
    self.presentingViewController?.dismiss(animated: false, completion: nil)
}

If Michael Voline's solution doesn't work , try the following code.如果 Michael Voline 的解决方案不起作用,请尝试以下代码。

let customViewController = self.presentingViewController as? CustomViewController

self.dismiss(animated: true) {

   customViewController?.dismiss(animated: true, completion: nil)

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM