Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS11 UIBarButtonItem action not get called

I used Xcode9 Beta6 to build the project, the action was called correctly on iOS10 device, however it is not work on iOS11 device.

In My project, there are some viewControllers have a UIToolBar on the top, and the toolBar contains some UIBarButtonItems.

There is one this kind of viewController, whose UIBarButtonItem action is not called when I tap the UIBarButtonItem. I can see the tapping animation (the icon become dim first and back to normal after finger released)

At the end of viewDidLoad, I print the info of toolbar.items to indicate that target action are set properly.

Debug Output

like image 680
Kan Chen Avatar asked Aug 28 '17 23:08

Kan Chen


1 Answers

In my case I was setting up the button and instantiating it as a property of the vc

class myVC: UIViewController {
let closeBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(close(_:)))
}

If I moved this to ViewDidLoad it resolved the problem

class myVC: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
    let closeBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(close(_:)))
}

}

like image 179
user499846 Avatar answered Sep 20 '22 13:09

user499846