注意:我只在模拟器上测试过。当UIBarButtonItem点击时,操作没有被调用
我想在代码中添加target-action而不是将它作为Interface Builder中的IBAction进行连接。
theButton
位于左上方的导航栏上,在IB中创建。
步骤:
声明
theButton
作为一个IBOutlet和IB连接它。添加这
viewDidLoad
:self.theButton.target = self; self.theButton.action = @selector(theAction);
我被这个测试
theAction
:- (void)theAction { NSLog(@"theAction called"); //do some other stuff }
当我在模拟器上点击theButton
,没有任何反应。我根本没有看到NSLog语句。
我错过了什么?
不需要,因为它是通过self.theButton.target = self以编程方式完成的;和self.theButton.action = @selector(theAction); – Russian
编辑我的答案,以编程方式使用。 – shannoga
请注意,* UIBarButtonItem *将不会响应* addTarget:action:forControlEvents:*方法 – EmptyStack