如何显示按钮的标签? [英] How show tag of button?
本文介绍了如何显示按钮的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的代码中,我想在控制台上显示每个按钮的标签.我为此尝试过,但它显示超出范围.怎么办?
In below code i want display tag of each button on console. I tried for that but it show out of scope. How do that?
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 33;
[btnMenu setTag:0 ];
for (int i = 1; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
//awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
btnMenu = [UIButton buttonWithType:UIButtonTypeCustom];
//NSData *data =UIImageJPEGRepresentation(, 1);
[btnMenu setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"page-%d.jpg",i]] forState:UIControlStateNormal];
CGRect frame = btnMenu.frame;
frame.size.width=320;
frame.size.height=460;
frame.origin.x=0;
frame.origin.y=0;
btnMenu.frame=frame;
[btnMenu setTag:i];
btnMenu.alpha = 1;
[btnMenu addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[awesomeView addSubview:btnMenu];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];
}
-(IBAction)btnSelected:(id)sender{
switch (btnMenu.tag) {
NSLog(@"%d",btnMenu.tag);
}}
推荐答案
试试这个:
-(IBAction)btnSelected:(id)sender{
UIButton *button = (UIButton *)sender;
int whichButton = button.tag;
NSLog(@"Current TAG: %i", whichButton);
}
您真的需要将该方法作为 IBAction 方法吗?
do you really need the method as an IBAction method?
你不能把它当作空的吗?
cannot you use it as a void one?
-(void)btnSelected:(id)sender{
UIButton *button = (UIButton *)sender;
int whichButton = button.tag;
NSLog(@"Current TAG: %i", whichButton);
}
这篇关于如何显示按钮的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文