ios - 以编程方式在 UIButton 上设置目标/操作?

标签 ios swift uibutton navigationbar

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

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

首先, Storyboard中添加的按钮会像这样设置它们的 Action :

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

这会正常工作,当用户单击关闭按钮时,我会像这样更改 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

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

最佳答案

似乎在:

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

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

确保您使用的是正确的目标

关于ios - 以编程方式在 UIButton 上设置目标/操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39820395/

相关文章:

ios - 如何将 UIView 转换为横向和全屏?

objective-c - Objective-C : Giving all objects of a class a pointer to a singleton of another class

ios - Sprite 套件将 SKAction 动画纹理限制在固定时间

ios - 按下时突出显示的UIButton不会在第一次按下时突出显示

ios - 有人可以解释 iOS "x-apple-msg-load://"吗?

ios - Swift 下一个按钮更改文本字段

arrays - '[任何? ]' is not convertible to ' 有什么东西吗?

ios - 从 SKScene 获取 UIViewController

iphone - 可缩放 UIScrollView 与 UIButtons 捕获触摸

ios - 是否可以在 Swift 的 UIButton 中添加两个标签?