我正在使用各种按钮,我已经从Xib文件设置了它的标签。并将所有按钮连接到单个方法-(void) note:(id)sender
。
现在我想要检索标签号。我可以看到点击了哪个按钮
-(void) note:(id)sender
{
NotesClass *note = [[NotesClass alloc] initWithNibName:@"NotesClass" bundle:nil];
note.notetag = sender;
NSLog(@"%@",note.notetag);
[self.navigationController pushViewController:note animated:YES];
}
打印那个NSlog时我得到这个输出:
<UIButton: 0x4e70350; frame = (227 119; 20 18); opaque = NO; autoresize = RM+BM; tag = 1; layer = <CALayer: 0x4e70480>>
任何人都可以帮助我。
答案 0 :(得分:12)
尝试以下代码,它一定会帮助你
UIButton *button = (UIButton *)sender;
NSInteger bTag = button.tag;
答案 1 :(得分:1)
您可以使用
获取标记sender.tag
答案 2 :(得分:1)
(void) note:(id)sender
{
NotesClass *note = [[NotesClass alloc] initWithNibName:@"NotesClass" bundle:nil];
note.notetag = [sender tag];
NSLog(@"%d",note.notetag);
[self.navigationController pushViewController:note animated:YES];
}
答案 3 :(得分:1)
-(void) note:(id)sender
{
NotesClass *note = [[NotesClass alloc] initWithNibName:@"NotesClass" bundle:nil];
note.notetag = [sender tag];
NSLog(@"%d",note.notetag);
//Another option is to use
UIButton *button = (UIButton *)sender;
NSLog(@"%d",button.tag);
[self.navigationController pushViewController:note animated:YES];
}
%d
而非%@
tag
属于int
类型
答案 4 :(得分:1)
in .H file write below code
@interface tagViewController : UIViewController {
UIButton *btn1;
}
@property(nonatomic,retain)IBOutlet UIButton *btn1;
-(IBAction)btnclicked:(id)sender;
@end
and in .M file write below code
-(IBAction)btnclicked:(id)sender
{
btn1 = (UIButton *)sender;
NSLog(@"You Press Button No %d",btn1.tag);
}
Don't forgate maping of your button Suppose i have three button and i set it tag 1,2,3 and then after mapping all of them with btnclicked: in TouchUp Inside Event and then after run it it's working...