简体   繁体   English

以编程方式设置UIButton的目标/操作?

[英]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. 在我的应用程序中,我有一个流程如下:用户按下一个更改UIBarButtonItems的按钮,当用户点击其中一个时,其更改为其他内容。 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: 当用户点击屏幕上其他位置的按钮时,我会像这样更改UIBarButtons:

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: 这可以正常工作,当用户单击关闭按钮时,我更改了UIBarButtonItems,如下所示:

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. 我已经尝试过UIBarButtonItem本身的.action,似乎也没有用。

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 . self (目标)实际上不是PBRevealViewController的实例,而是MapViewController That means that you are telling the button to call method revealLeftView on a MapViewController . 这意味着你告诉按钮在MapViewController上调用方法revealLeftView

Make sure you are using the correct target . 确保使用正确的target

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

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