简体   繁体   English

UIBarbuttonItem解雇UIVIewcontroller

[英]UIBarbuttonItem dismiss UIVIewcontroller

I added an extension to UIViewController to add a close button 我添加了一个UIViewController的扩展来添加一个关闭按钮

    extension UIViewController {
      func addCloseButton() {
            let button = UIBarButtonItem(image: #imageLiteral(resourceName: "bar_close"), 
                                         landscapeImagePhone: nil,
                                         style: .done,
                                         target: self, 
                                         action: #selector(UIViewController.dismiss(animated:completion:)))
            navigationItem.leftBarButtonItem = button
      }
    } 

When i tap the barbutton i get a crash directly to AppDelegate. 当我点击按钮时,我直接崩溃到AppDelegate。 Any hints? 任何提示? Seems related to the selector. 似乎与选择器有关。

You can't use dismiss(animated:completion:) as selector here because it takes two arguments bool and closure and bar button action pass args as UIBarButtonItem which cause app crash. 你不能在这里使用dismiss(animated:completion :)作为选择器,因为它需要两个参数bool和closure以及bar按钮动作传递args作为UIBarButtonItem导致应用程序崩溃。 so change your code like this. 所以改变你的代码就像这样。

extension UIViewController {
    func addCloseButton() {
        let button = UIBarButtonItem(image: #imageLiteral(resourceName: "rightgreen"),
                                 landscapeImagePhone: nil,
                                 style: .done,
                                 target: self,
                                 action: #selector(onClose))
        navigationItem.leftBarButtonItem = button
    }

    @objc func onClose(){
        self.dismiss(animated: true, completion: nil)
    }
}

However this question has accepted answer which load extra one method addCloseButton in each and every viewcontroller still posting a answer will going to help someone 然而,这个问题已经接受了在每个addCloseButton中加载额外一个方法addCloseButton的答案仍然发布答案将帮助某人

NOTE : This example for adding barbutton item automatically and also handle action for pop view controller. 注意:此示例用于自动添加barbutton项目并处理pop视图控制器的操作。

As Protocol extension doesn't provide a to implement selector methods so to get the rid of it I have created this solution. 由于协议扩展不提供实现选择器方法,所以为了摆脱它,我已经创建了这个解决方案。

First thing you need is BaseVC which is subclass of UIViewController and all of your view controller going to be inherited by BaseVC like your class LoginVC:BaseVC ... 你需要的第一件事是BaseVC ,它是UIViewController子类,你的所有视图控制器都将由BaseVC继承,就像您的class LoginVC:BaseVC ...

now declare protocol 现在声明协议

protocol PopableClass {
  func popSelf (animated:Bool)

}

extension PopableClass where Self : UIViewController {

    func popSelf (animated:Bool)  {
        self.navigationController?.popViewController(animated: animated)
    }

}

In your Base VC add two methods and call setupNavigationBar from viewDidLoad 在Base VC中添加两个方法并从viewDidLoad调用setupNavigationBar

func setupNavigationBar () {

    if self is PopableClass {
        let barbuttonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "back"), landscapeImagePhone: #imageLiteral(resourceName: "back"), style: .plain, target: self, action: #selector(popViewController))
        self.navigationItem.leftBarButtonItem = barbuttonItem
    }
}

//--------------------------------------------------------------------------------

@objc func popViewController () {
    if self is PopableClass {
        (self as! PopableClass).popSelf(animated: true)
    }
}

You did it !! 你做到了 !!

Now in whatever class you need back button to pop view controller just use like this 现在在任何类别你需要后退按钮弹出视图控制器就像这样使用

class PushedClass: BaseVC,PopableClass

Hope it is helpful 希望它有所帮助

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

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