【发布时间】:2015-02-16 16:25:26
【问题描述】:
我不明白为什么标签第三次等于 0。
UIButton *b1;
- (void)viewDidLoad
{
b1 =[[UIButton alloc] init];
b1.tag = 1;
NSLog(@"Button pressed: %d", b1.tag); // tag = 1
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)funcA:(id)sender // I create mannualy the button b1
{
NSLog(@"Button pressed 2nd: %d", b1.tag); // tag = 1
b1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b1.frame = CGRectMake(50, 50, 50, 50);
[b1 setTitle:@"b1" forState:UIControlStateNormal];
[self.view addSubview:b1];
[b1 addTarget:self action:@selector(funcB:) forControlEvents:UIControlEventTouchUpInside ];
}
-(void)funcB:(id)sender //the func of B1
{
NSLog(@"Button 3rd %d", b1.tag); // here the tag = 0
}
我希望我的要求是可能的。 ^^
【问题讨论】:
-
对不起,b1 在顶部,我只是失败了。那么如果 B1 是一个全局变量,它好吗?而且我不明白问题c:三个按钮绑定一个动作,我尝试区分用户按下了哪个按钮,为此,我使用他们的标签。
-
如果你有三个不同的按钮都调用同一个动作方法,你可以(a)给三个视图单独的
tag值和(b)查看sender参数,这将参考哪个按钮被按下。在回答您的问题时,如果它确实是全局变量,那可能不是一个好主意。如果每个按钮都有单独的类的实例变量(或者更好的是一个属性),那么如果您需要保留对它的引用以供将来使用,那很好。 -
是的,我别无选择,我让我做一个财产。感谢您的帮助。现在好了。
标签: objective-c uibutton tags