ios 如何在 Swift 中关闭 ViewController?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24668818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:18:39  来源:igfitidea点击:

How to dismiss ViewController in Swift?

iosswiftviewcontroller

提问by rshankar

I am trying to dismiss a ViewController in swift by calling dismissViewControllerin an IBAction

我试图通过调用dismissViewController一个IBAction

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

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

random image of a segue

转场的随机图像

I could see the println message in console output but ViewController never gets dismissed. What could be the problem?

我可以在控制台输出中看到 println 消息,但 ViewController 永远不会被解雇。可能是什么问题呢?

回答by Zoon Nooz

From you image it seems like you presented the ViewController using push

从您的图像看来,您似乎使用 push 呈现了 ViewController

The dismissViewControllerAnimatedis used to close ViewControllers that presented using modal

dismissViewControllerAnimated用于使用模式所呈现接近ViewControllers

Swift 2

斯威夫特 2

navigationController.popViewControllerAnimated(true)

Swift 4

斯威夫特 4

navigationController?.popViewController(animated: true)

dismiss(animated: true, completion: nil)

回答by satheesh

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)

回答by BaSha

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

回答by Chase McElroy

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)

回答by LAOMUSIC ARTS

  1. embed the View you want to dismiss in a NavigationController
  2. add a BarButton with "Done" as Identifier
  3. invoke the Assistant Editor with the Done button selected
  4. create an IBAction for this button
  5. add this line into the brackets:

    self.dismissViewControllerAnimated(true, completion: nil)
    
  1. 将要关闭的视图嵌入到 NavigationController 中
  2. 添加一个带有“完成”作为标识符的 BarButton
  3. 在选中“完成”按钮的情况下调用“助理编辑器”
  4. 为这个按钮创建一个 IBAction
  5. 将此行添加到括号中:

    self.dismissViewControllerAnimated(true, completion: nil)
    

回答by jobima

Use:

用:

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

instead of:

代替:

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

回答by Peyotle

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 将不为零并且代码将被执行。

回答by David.Chu.ca

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 UIViewControllersubclass. For example, in cancel action:

然后我调用这个方法来关闭任何子UIViewController类中的视图控制器。例如,在取消操作中:

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

回答by OhadM

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 dismissmethod from it self.

因此,仅从自身调用dismiss方法是一种不好的做法。

What you should do if you're presenting it modal is:

如果您以模态呈现它,您应该做的是:

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

回答by Fatih

Don't create any segue from Cancel or Done to other VCand only write this code your buttons @IBAction

不要创建从 Cancel 或 Done 到其他 VC 的任何 segue并且只编写此代码您的按钮 @IBAction

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