iPhone:我可以将类方法用于 UIBarButtonItem 的 Action 选择器吗?

标签 iphone objective-c ios uikit

我想为 UIBarButtonItem 的 Action 选择器使用类方法。

代码是:

[[UIBarButtonItem alloc] 
    initWithTitle:@"title"                                            
    style:UIBarButtonItemStylePlain
    target: self
    action:@selector(method)]

当我使用实例方法进行操作时,它起作用了。

但是当我使用类方法进行操作时,点击按钮时会发生错误。
错误消息是:unrecognized selector sent to instance

我不能为此使用类方法吗?
如何设置 use for target,而不是 self?

最佳答案

请更改为以下代码:
这里的target应该是self而不是[self class]

[[UIBarButtonItem alloc] initWithTitle:@"title"
                                 style:UIBarButtonItemStylePlain
                                target:self
                                action:@selector(method)];

关于iPhone:我可以将类方法用于 UIBarButtonItem 的 Action 选择器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10930312/

相关文章:

iphone - ObjectiveFlickr - 如何在响应之前未知的照片 ID 上调用 getSizes?

iphone - 如何在 XCode/iOS 中包含 HTML 文件?

ios - "Please use a provisioning profile associated with team ID"尝试分发IOS应用程序时

ios - Apple Store-iTunes连接-每种设备类型一个版本

iphone - 设置 UIPopoverController 的 "title"?

objective-c - 获取实体类型 3 的共享日历邀请时出错,Xcode 6.1.1。 EKCalender、EKSource、EKEventstore 和 Objective C

iphone - Three20 应用商店拒绝 (UITouch) - 删除了违规代码,发布版本但以二进制形式存在

objective-c - 我可以将两个 UIColor 混合在一起吗?

ios - objective-c/ios 跟踪屏幕使用情况

ios - 读取文件后 Objective-C 因 EXC_BAD_ACCESS 崩溃