Add target to stock back button in navigationBar

前端 未结 6 1871
我在风中等你
我在风中等你 2021-01-14 18:48

I am setting the values of the title and back button in the UINavigationBar as follows:

self.navigationItem.title = @\"Post\";
[self.navigationC         


        
6条回答
  •  攒了一身酷
    2021-01-14 19:25

    You cannot add target to stock BackButton, but you can override a UINavigationBarDelegate method in your CustomNavigationController, like this:

    class CustomNavigationController: UINavigationController {
    
    @IBOutlet weak var ibNavigationBar: UINavigationBar!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            ibNavigationBar.delegate = self
        }
    
    }
    
    extension CustomNavigationController: UINavigationBarDelegate {
        func navigationBar(navigationBar: UINavigationBar, shouldPopItem item: UINavigationItem) -> Bool {
            for viewController in viewControllers {
                if viewController.title == "YourTitledViewController" {
                    popToViewController(viewController, animated: true)
                }
            }
            return false
        }
    }
    

提交回复
热议问题