如何在Swift中解除ViewController?

rsh*_*kar 197 viewcontroller ios swift

我试图通过调用dismissViewController一个来解除swift中的ViewControllerIBAction

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

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

segue的随机图像

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

Zoo*_*ooz 386

从你的图像看起来你似乎使用push呈现了ViewController

dismissViewControllerAnimated用于关闭使用模式所呈现ViewControllers

斯威夫特2

navigationController.popViewControllerAnimated(true)
Run Code Online (Sandbox Code Playgroud)

斯威夫特4

navigationController?.popViewController(animated: true)

dismiss(animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

  • 对于**Swift 3**navigationController!.popViewController(动画:true) (7认同)
  • 你会怎么做'显示细节'segue? (6认同)
  • 对于**Swift 2.2**navigationController!.popViewControllerAnimated(true) (2认同)

sat*_*esh 166

我有一个解决你的问题的方法.如果使用模态显示视图,请尝试使用此代码关闭视图控制器:

斯威夫特3:

self.dismiss(animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

要么

如果您使用"push"segue显示视图

self.navigationController?.popViewController(animated: true)
Run Code Online (Sandbox Code Playgroud)

  • 如果你对我的问题没有反应,谈话就没用了. (28认同)
  • 这与OP使用的方法有何不同? (4认同)

BaS*_*Sha 19

如果你这样做我猜你可能不会在控制台中收到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")
  }    
}
Run Code Online (Sandbox Code Playgroud)


LAO*_*RTS 13

  1. 在NavigationController中嵌入要关闭的视图
  2. 添加一个带有"Done"作为标识符的BarButton
  3. 选中"完成"按钮调用"助理编辑器"
  4. 为此按钮创建一个IBAction
  5. 将此行添加到括号中:

    self.dismissViewControllerAnimated(true, completion: nil)
    
    Run Code Online (Sandbox Code Playgroud)


Cha*_*roy 12

在Swift 3.0到4.0中,就像在函数中键入它一样简单:

self.dismiss(animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

或者如果你在导航控制器中,你可以"弹出"它:

self.navigationController?.popViewController(animated: true)
Run Code Online (Sandbox Code Playgroud)


job*_*ima 11

使用:

self.dismiss(animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

代替:

self.navigationController.dismissViewControllerAnimated(true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

  • 添加一个!到 navigationController 它对我有用 (2认同)
  • @naturalc:请注意,如果 navigationController 为零并且您输入 !,应用程序将崩溃 (2认同)

Pey*_*tle 7

如果您在没有导航控制器的情况下展示控制器,则可以从所提供控制器的方法调用以下代码.

self.presentingViewController?.dismiss(animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

如果您的ViewController是以模态方式呈现的,则可选的presentsViewController将不是nil,代码将被执行.


Dav*_*.ca 6

根据我的经验,我添加了一个方法来解雇我作为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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我调用此方法来解除任何子UIViewController类中的视图控制器.例如,在取消操作中:

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


Sai*_*ddy 6

所以如果你想关闭你的 Viewcontroller 使用这个。这段代码写在按钮动作中,用于关闭 VC

  @IBAction func cancel(sender: AnyObject) {
   dismiss(animated: true, completion: nil)
  }
Run Code Online (Sandbox Code Playgroud)


Oha*_*adM 6

从Apple 文档中

呈现视图控制器负责解散其呈现的视图控制器

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

如果要呈现模式,应该做的是:

presentingViewController?.dismiss(animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)


小智 6

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

self.dismiss(animated: false, completion: nil)
Run Code Online (Sandbox Code Playgroud)


小智 5

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

@IBAction func cancel(sender: AnyObject) {
    dismiss(animated: false, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)


Far*_*dov 5

如果您在父 VC 中使用 Present 方法,那么您应该调用此函数,以关闭子 VC 使用此函数

self.dismiss(animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

如果你使用push方法调用子VC,要关闭子VC使用这个

self.navigationController?.popViewController(animated: true)
Run Code Online (Sandbox Code Playgroud)