如何使用UIButtonBarItem触发方法? [英] How to trigger methods with UIButtonBarItem?
问题描述
我有一个带有工具栏的xib,在那上面我有2个按钮,都是用IB制作的.我可以将插座连接到按钮,但是单击它们时不会触发该方法吗?为什么会这样?
I have a a xib with a toolbar and on that I have 2 buttons all made in IB. I can connect my outlets to the buttons but when I click them the method isn't triggered? Why is that?
在我的头文件中,我有这个:
In my header file I have this:
@property (nonatomic, retain) IBOutlet UIBarButtonItem *cancelEntry;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *saveEntry;
-(IBAction) cancelEntry:(id) sender;
-(IBAction) saveEntry:(id) sender;
在我的.m文件中,我得到了:
In my .m file I got this:
-(IBAction) cancelEntry:(id) sender {
NSLog(@"cancel");
}
-(IBAction) saveEntry:(id) sender {
NSLog(@"save");
}
所有编译均应按原样进行,但单击按钮时日志中什么也没有.我应该怎么做才能使其正常工作?
All compiles as it should but no nothing in the log when clicking the buttons. What should I do to get it to work?
推荐答案
听起来就像您将插座连接到按钮,但未将按钮连接到动作.
Sounds like you connected your outlets to the buttons, but didn't connect the buttons to the actions.
- 右键单击"Interface Builder"中的
UIBarButtonItem
. - 将鼠标悬停在选择器上,然后将显示在文件所有者上的+拖动.
- 将其连接到cancelEntry:.
- 重复保存.
代替在您的IBAction中使用(id)
,您应该使用(UIBarButtonItem *)
.这样可以防止您将其他对象类型连接到那些IBAction.
Instead of using (id)
for your IBActions, you should probably use (UIBarButtonItem *)
. That'll prevent you from connecting a different object type to those IBActions.
这篇关于如何使用UIButtonBarItem触发方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!