将操作添加到自定义视图中的 UIButton
我有一个自定义视图,它有一个 UIButton。我将此视图添加到我的 navigationItem 的 rightbarButtonItem 中,可以,但是我必须向此按钮添加一个操作。
这是我的代码;
-(void)showMessage:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"next" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
}
- (void)viewDidLoad {
UpDownButtonView *buttonView = [[UpDownButtonView alloc] initWithNibName:@"UpDownButtonView" bundle:nil];
[buttonView.upButton addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
[buttonView.downButton addTarget:self action:@selector(prev:) forControlEvents:UIControlEventTouchUpInside];
buttonView.view.frame = CGRectMake(0,0,95,34);
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:buttonView.view];
self.navigationItem.rightBarButtonItem = item;
[self setTitle:@"Details"];
[super viewDidLoad];
}
谢谢。
I have a custom view and it has a UIButton. I add this view to my navigationItem's rightbarButtonItem, it's ok, but I have to add an action to this button.
Here is my code;
-(void)showMessage:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"next" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
}
- (void)viewDidLoad {
UpDownButtonView *buttonView = [[UpDownButtonView alloc] initWithNibName:@"UpDownButtonView" bundle:nil];
[buttonView.upButton addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
[buttonView.downButton addTarget:self action:@selector(prev:) forControlEvents:UIControlEventTouchUpInside];
buttonView.view.frame = CGRectMake(0,0,95,34);
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:buttonView.view];
self.navigationItem.rightBarButtonItem = item;
[self setTitle:@"Details"];
[super viewDidLoad];
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要带有 UIButton 的自定义视图,您需要的是分配并初始化 UIBarButtonItem 并将其放在导航栏上,
代码如下所示:
You don't need a custom view with a UIButton, what you need is alloc and init a UIBarButtonItem and put it on your nav bar
the code would look like: