点击UIToolbar上的UIBarButtonItem如何检测是点击哪一个
1个回答
展开全部
我自己用xcode模板生成一个程序,然后把root view controller添加以下代码,发现应该可以用tag来区分啊?selector函数中传入的sender是一个UIBarButtonItem.
我用的是viewcontroller自带的toolbar。不过道理应该一样吧。
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* b1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(onToolbarButtonTapped:)];
b1.tag = 2010;
UIBarButtonItem* b2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(onToolbarButtonTapped:)];
b2.tag = 2011;
self.navigationController.toolbarHidden = FALSE;
NSArray* buttons = [NSArray arrayWithObjects:
b1,
b2,
NULL];
self.toolbarItems = buttons;
[b1 release];
[b2 release];
}
- (void) onToolbarButtonTapped:(id)sender {
NSLog(@"sender: %@, tag = %d", sender, [sender tag]);
switch([sender tag]) {
case 2010:
NSLog(@"add button");
break;
case 2011:
NSLog(@"action button");
break;
default:
NSLog(@"unknown button");
}
}
运行时,先点一下第一个按纽,再点一下第二个,输出为:
2010-07-21 15:36:35.587 [20852:207] sender: <UIBarButtonItem: 0x5957040>, tag = 2010
2010-07-21 15:36:35.589 [20852:207] add button
2010-07-21 15:36:36.041 [20852:207] sender: <UIBarButtonItem: 0x5953bb0>, tag = 2011
2010-07-21 15:36:36.043 [20852:207] action button
我用的是viewcontroller自带的toolbar。不过道理应该一样吧。
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* b1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(onToolbarButtonTapped:)];
b1.tag = 2010;
UIBarButtonItem* b2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(onToolbarButtonTapped:)];
b2.tag = 2011;
self.navigationController.toolbarHidden = FALSE;
NSArray* buttons = [NSArray arrayWithObjects:
b1,
b2,
NULL];
self.toolbarItems = buttons;
[b1 release];
[b2 release];
}
- (void) onToolbarButtonTapped:(id)sender {
NSLog(@"sender: %@, tag = %d", sender, [sender tag]);
switch([sender tag]) {
case 2010:
NSLog(@"add button");
break;
case 2011:
NSLog(@"action button");
break;
default:
NSLog(@"unknown button");
}
}
运行时,先点一下第一个按纽,再点一下第二个,输出为:
2010-07-21 15:36:35.587 [20852:207] sender: <UIBarButtonItem: 0x5957040>, tag = 2010
2010-07-21 15:36:35.589 [20852:207] add button
2010-07-21 15:36:36.041 [20852:207] sender: <UIBarButtonItem: 0x5953bb0>, tag = 2011
2010-07-21 15:36:36.043 [20852:207] action button
富港检测技术(东莞)有限公司_
2024-05-27 广告
2024-05-27 广告
ISTA3E程序是对相同产品的集合包装的综合模拟性能测试,集合包装件被定义为将一个产品、多个产品或包装件放置在滑板或托盘上,固定在一起或是作为一个单元运输。例如:一台机器由带瓦楞底托的托盘上、瓦楞侧围、顶盖包装,用缠绕膜缠绕在托盘上。用于评...
点击进入详情页
本回答由富港检测技术(东莞)有限公司_提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询