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

时间:2016-10-02 18:44:37

标签: ios swift uibutton navigationbar

在我的应用程序中,我有一个流程如下:用户按下一个更改UIBarButtonItems的按钮,当用户点击其中一个时,其更改为其他内容。但是,当点击其中一个时,我得到以下异常。

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

首先,故事板中添加的按钮的动作设置如下:

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

当用户点击屏幕上其他位置的按钮时,我会像这样更改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

这样可以正常工作,当用户点击关闭按钮时,我会像这样更改UIBarButtonItem:

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

它显示一切都很好,但是当我点击其中一个时,它会抛出上面提到的异常。我究竟做错了什么?我已经在UIBarButtonItem本身上尝试了.action,它似乎也无法工作。

1 个答案:

答案 0 :(得分:0)

似乎在:

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

self(目标)实际上不是PBRevealViewController的实例,而是MapViewController的实例。这意味着您正在告诉按钮在revealLeftView上调用方法MapViewController

确保使用正确的target