如何将一个方法与这个按钮关联起来?
我有一个显示图像和文本的导航栏按钮。这是代码:
UIImage *saveImage = [UIImage imageNamed:@"star.png"];
UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
[saveButton setBackgroundImage:saveImage forState:UIControlStateNormal];
[saveButton setTitle:@"Save" forState:UIControlStateNormal];
saveButton.frame = (CGRect) {
.size.width = 100,
.size.height = 30,
};
UIBarButtonItem *barButton= [[UIBarButtonItem alloc] initWithCustomView:saveButton];
[self.navigationItem setRightBarButtonItem:barButton animated:YES];
我尝试过
[barButton setAction:@selector(saveArray)];
,但不起作用。
I have a navigation bar button that displays both image and text. This is the code:
UIImage *saveImage = [UIImage imageNamed:@"star.png"];
UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
[saveButton setBackgroundImage:saveImage forState:UIControlStateNormal];
[saveButton setTitle:@"Save" forState:UIControlStateNormal];
saveButton.frame = (CGRect) {
.size.width = 100,
.size.height = 30,
};
UIBarButtonItem *barButton= [[UIBarButtonItem alloc] initWithCustomView:saveButton];
[self.navigationItem setRightBarButtonItem:barButton animated:YES];
I tried this
[barButton setAction:@selector(saveArray)];
but it doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请记住,您还必须指定目标。您可以将操作/目标设置为 UIButton 对象本身:
我尝试直接将目标/操作设置为 UIBarButtonItem,但由于某种原因,在 UIButton 用于自定义视图的情况下似乎不起作用。
Remember that you must specify target as well. You can set action/target to the UIButton object itself:
I've tried to set target/action to the UIBarButtonItem directly but it seems not to work in case of UIButton for custom view for some reason.
你记得设定目标吗?如果所需的方法属于同一个类,您可以这样做:
也就是说,
action
和target
都是属性(Obj-C 2.0 的新功能),请考虑使用点符号:Did you remember to set the target? If the desired method belongs to the same class, you could do:
That said,
action
andtarget
are both properties (new to Obj-C 2.0), consider using the dot notation: