简体   繁体   中英

Setting target/action on UIButton programmatically?

In my app I have a flow which goes like: The user press a button which changes the UIBarButtonItems and when the user clicks on one of them, its changes to something else. However I get the following exception when click on one of them.

  *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[App.MapViewController revealLeftView]: unrecognized selector sent to instance 0x7fbc90c404c0'

First the buttons added in the storyboard gets their action set like this:

menuButton.action = #selector(PBRevealViewController.revealLeftView)
toolboxMenuButton.action = #selector(PBRevealViewController.revealRightView)

When the user clicks on a button elsewhere on the screen, I change the UIBarButtons like this:

let leftButton: UIButton = UIButton(type: UIButtonType.custom)
//set image for button
leftButton.setImage(UIImage(named: "close"), for: UIControlState.normal)
//add function for button
leftButton.addTarget(self, action: #selector(MapViewController.cancelAddFields), for: UIControlEvents.touchUpInside)
//set frame
leftButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
let leftBarButton = UIBarButtonItem(customView: leftButton)

let rightButton: UIButton = UIButton(type: UIButtonType.custom)
//set image for button
rightButton.setImage(UIImage(named: "add"), for: UIControlState.normal)
//add function for button
rightButton.addTarget(self, action: #selector(MapViewController.confirmCreateFields(sender:)), for: UIControlEvents.touchUpInside)
//set frame
rightButton.frame = CGRect(x: 0, y: 0, width: 24, height: 20)
let rightBarButton = UIBarButtonItem(customView: rightButton)

//assign button to navigationbar
self.navigationItem.rightBarButtonItem = rightBarButton
self.navigationItem.leftBarButtonItem = leftBarButton

This works as it should, and when the user clicks the close-button, I change the UIBarButtonItems like this:

let leftButton: UIButton = UIButton(type: UIButtonType.custom)
        //set image for button
        leftButton.setImage(UIImage(named: "MenuIcon_1"), for: UIControlState.normal)
        //add function for button
        //leftButton.addTarget(self, action: #selector(MapViewController.cancelAddFields), for: UIControlEvents.touchUpInside)
        //set frame
        leftButton.frame = CGRect(x: 0, y: 0, width: 24, height: 24)

        let rightButton: UIButton = UIButton(type: UIButtonType.custom)
        //set image for button
        rightButton.setImage(UIImage(named: "ToolsIcon_1"), for: UIControlState.normal)
        //add function for button
        //rightButton.addTarget(self, action: #selector(MapViewController.cancelAddFields), for: UIControlEvents.touchUpInside)
        //set frame
        rightButton.frame = CGRect(x: 0, y: 0, width: 24, height: 24)

        leftButton.addTarget(self, action: #selector(PBRevealViewController.revealLeftView), for: UIControlEvents.touchUpInside)
        rightButton.addTarget(self, action: #selector(PBRevealViewController.revealRightView), for: UIControlEvents.touchUpInside)

        let leftBarButton = UIBarButtonItem(customView: leftButton)
        let rightBarButton = UIBarButtonItem(customView: rightButton)
        //assign button to navigationbar
        self.navigationItem.rightBarButtonItem = rightBarButton
        self.navigationItem.leftBarButtonItem = leftBarButton

It shows all up good, but when I click on one of them it throws the exception mentioned above. What am I doing wrong? I've tried the .action on the UIBarButtonItem itself, doesn't seem to work either.

It seems that in:

leftButton.addTarget(self, action: #selector(PBRevealViewController.revealLeftView), for: UIControlEvents.touchUpInside)

self (the target) is not actually an instance of PBRevealViewController but a MapViewController . That means that you are telling the button to call method revealLeftView on a MapViewController .

Make sure you are using the correct target .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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