简体   繁体   English

如何在 Swift 中关闭 ViewController?

[英]How to dismiss ViewController in Swift?

I am trying to dismiss a ViewController in swift by calling dismissViewController in an IBAction我试图通过在IBAction调用dismissViewController来迅速解除一个ViewController

  @IBAction func cancel(sender: AnyObject) {
    self.dismissViewControllerAnimated(false, completion: nil)
    println("cancel")
}

@IBAction func done(sender: AnyObject) {
    self.dismissViewControllerAnimated(false, completion: nil)
    println("done")
}

转场的随机图像

I could see the println message in console output but ViewController never gets dismissed.我可以在控制台输出中看到 println 消息,但 ViewController 永远不会被解雇。 What could be the problem?可能是什么问题呢?

From you image it seems like you presented the ViewController using push从您的图像看来,您似乎使用 push 呈现了 ViewController

The dismissViewControllerAnimated is used to close ViewControllers that presented using modal dismissViewControllerAnimated用于关闭使用 modal 呈现的 ViewControllers

Swift 2斯威夫特 2

navigationController.popViewControllerAnimated(true)

Swift 4斯威夫特 4

navigationController?.popViewController(animated: true)

dismiss(animated: true, completion: nil)

I have a solution for your problem.我有你的问题的解决方案。 Please try this code to dismiss the view controller if you present the view using modal:如果您使用模态显示视图,请尝试使用此代码关闭视图控制器:

Swift 3:斯威夫特 3:

self.dismiss(animated: true, completion: nil)

OR或者

If you present the view using "push" segue如果您使用“推”转场呈现视图

self.navigationController?.popViewController(animated: true)

if you do this i guess you might not get println message in console,如果您这样做,我想您可能不会在控制台中收到 println 消息,

@IBAction func cancel(sender: AnyObject) {
  if(self.presentingViewController){
    self.dismissViewControllerAnimated(false, completion: nil)
    println("cancel")
   }
}

@IBAction func done(sender: AnyObject) {
  if(self.presentingViewController){
    self.dismissViewControllerAnimated(false, completion: nil)
    println("done")
  }    
}

In Swift 3.0 to 4.0 it's as easy as typing this into your function:在 Swift 3.0 到 4.0 中,只需在函数中输入以下内容即可:

self.dismiss(animated: true, completion: nil)

Or if you're in a navigation controller you can "pop" it:或者,如果您在导航控制器中,您可以“弹出”它:

self.navigationController?.popViewController(animated: true)
  1. embed the View you want to dismiss in a NavigationController将要关闭的视图嵌入到 NavigationController 中
  2. add a BarButton with "Done" as Identifier添加一个带有“完成”作为标识符的 BarButton
  3. invoke the Assistant Editor with the Done button selected在选中“完成”按钮的情况下调用“助理编辑器”
  4. create an IBAction for this button为这个按钮创建一个 IBAction
  5. add this line into the brackets:将此行添加到括号中:

     self.dismissViewControllerAnimated(true, completion: nil)

Use:用:

self.dismiss(animated: true, completion: nil)

instead of:代替:

self.navigationController.dismissViewControllerAnimated(true, completion: nil)

If you presenting a controller without a Navigation Controller, you can call the following code from a method of the presented controller.如果您呈现一个没有导航控制器的控制器,您可以从呈现的控制器的方法中调用以下代码。

self.presentingViewController?.dismiss(animated: true, completion: nil)

If your ViewController is presented modally, optional presentingViewController will be not nil and the code will be executed.如果您的 ViewController 以模态呈现,则可选的presentingViewController 将不为零并且代码将被执行。

From Apple documentations :来自 Apple 文档

The presenting view controller is responsible for dismissing the view controller it presented呈现视图控制器负责解除它呈现的视图控制器

Thus, it is a bad practise to just invoke the dismiss method from it self.因此,仅从自身调用dismiss方法是一种不好的做法。

What you should do if you're presenting it modal is:如果您以模态呈现它,您应该做的是:

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

Based on my experience, I add a method to dismiss me as extension to UIViewController:根据我的经验,我添加了一个方法来将我解雇为 UIViewController 的扩展:

extension UIViewController {
    func dismissMe(animated: Bool, completion: (()->())?) {
        var count = 0
        if let c = self.navigationController?.viewControllers.count {
            count = c
        }
        if count > 1 {
            self.navigationController?.popViewController(animated: animated)
            if let handler = completion {
                handler()
            }
        } else {
            dismiss(animated: animated, completion: completion)
        }
    }
}

Then I call this method to dismiss view controller in any UIViewController subclass.然后我调用这个方法来关闭任何UIViewController子类中的视图控制器。 For example, in cancel action:例如,在取消操作中:

class MyViewController: UIViewController {
   ...
   @IBAction func cancel(sender: AnyObject) {
     dismissMe(animated: true, completion: nil)
   }
   ...
}

Don't create any segue from Cancel or Done to other VC and only write this code your buttons @IBAction不要创建从 Cancel 或 Done 到其他 VC 的任何 segue并且只编写此代码您的按钮 @IBAction

@IBAction func cancel(sender: AnyObject) {
    dismiss(animated: false, completion: nil)
}

Here is the one way to dismiss present view controller and move back to previous view controller.这是关闭当前视图控制器并返回到前一个视图控制器的一种方法。 You can do this through Storyboard only.您只能通过 Storyboard 执行此操作。

  1. Open Storyboard打开故事板
  2. Right click on Cancel button and drag it to previous view controller, where you want to move back to previous controller右键单击“取消”按钮并将其拖动到上一个视图控制器,您要在此处移回上一个控制器
  3. Now release the right click and you can see some actions which performs on cancel button现在释放右键单击,您可以看到在取消按钮上执行的一些操作
  4. Now choose "popover present" option from list现在从列表中选择“popover present”选项
  5. Now you can dismiss your current view by click on cancel button现在您可以通过单击取消按钮关闭当前视图

Please try this, It's working with me.请试试这个,它正在与我合作。

Second Way - Use - navigationController.popViewControllerAnimated(true)第二种方式 - 使用 - navigationController.popViewControllerAnimated(true)

Best luck..好运..

由于您使用了推送呈现的视图控制器,因此,您可以使用

self.dismiss(animated: false, completion: nil)

For reference, be aware that you might be dismissing the wrong view controller.作为参考,请注意您可能会忽略错误的视图控制器。 For example, if you have an alert box or modal showing on top of another modal.例如,如果您有一个警告框或模态显示在另一个模态之上。 (You could have a Twitter post alert showing on top of your current modal alert, for example). (例如,您可以在当前的模式警报之上显示 Twitter 帖子警报)。 In this case, you need to call dismiss twice, or use an unwind segue.在这种情况下,您需要调用两次dismiss,或者使用unwind segue。

So if you wanna dismiss your Viewcontroller use this.所以如果你想关闭你的 Viewcontroller 使用这个。 This code is written in button action to dismiss VC此代码是在按钮操作中编写以关闭 VC

  @IBAction func cancel(sender: AnyObject) {
   dismiss(animated: true, completion: nil)
  }

Try this:尝试这个:

@IBAction func close() {
  dismiss(animated: true, completion: nil)
}

If you using the present method in the parent VC then you should call this function, to dismiss the child VC use this如果您在父 VC 中使用当前方法,则应调用此函数,以解除子 VC 使用此方法

self.dismiss(animated: true, completion: nil)

if you calling child VC by using push method, to dismiss the child VC use this如果您使用 push 方法调用子 VC,要关闭子 VC,请使用此方法

self.navigationController?.popViewController(animated: true)

如果您以模态方式呈现一个 ViewController,并且想返回到根 ViewController,请在返回根 ViewController 之前注意关闭这个以模态方式呈现的 ViewController,否则该 ViewController 将不会从内存中删除并导致内存泄漏。

In Swift 3.0在 Swift 3.0 中

If you want to dismiss a presented view controller如果你想关闭一个呈现的视图控制器

self.dismiss(animated: true, completion: nil)

In Swift 4.1 and Xcode 9.4.1在 Swift 4.1 和 Xcode 9.4.1 中

If you use pushViewController to present new view controller, use this如果您使用 pushViewController 来呈现新的视图控制器,请使用此

self.navigationController?.popViewController(animated: false)
@IBAction func back(_ sender: Any) {
        self.dismiss(animated: false, completion: nil)
    }

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

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