如何识别以编程方式创建的按钮标签
How to identify programmatically created button tags
我正在使用 for 循环来标记和标注 16 个按钮。我正在尝试确定点击了哪个按钮。但是每当我点击一个按钮时,我的应用程序就会终止
due to uncaught exception ’NSInvalidArgumentException’, reason: ‘-[LocalView buttonTapped:]: unrecognized selector sent to instance 0x7ff38a077800’.
Using a Mutable Array has been suggested 作为识别按钮标签的一种方式,但我不明白为什么当 属性 已经定义时有必要这样做。
我需要标签在 1 到 16 范围内,但由于我不明白的原因,NSLog 迫使我使用 %li
而不是 %i
来记录每个标签。
这是我修改后的代码 select 一个按钮(编辑 1)
- (void)buttonTapped:(id)sender{
UIButton *tappedButton = (UIButton *)sender;
// [tappedButton setTag: tappedButton.tag]; EDIT 4
NSLog(@"tapped Button %i", tappedButton.tag);
// tag should select 1 of 16 states
switch (tappedButton.tag) {
case 1:
[self goToWait1];
break;
case 2:
[self goToWait2];
break;
case 3:
[self goToWait3];
break;
case 4:
[self goToWait4];
break;
case 5:
[self goToWait5];
break;
case 6:
[self goToWait6];
break;
case 7:
[self goToWait7];
break;
case 8:
[self goToWait8];
break;
case 9:
[self goToWait9];
break;
case 10:
[self goToWait10];
break;
case 11:
[self goToWait11];
break;
case 12:
[self goToWait12];
break;
case 13:
[self goToWait13];
break;
case 14:
[self goToWait14];
break;
case 15:
[self goToWait15];
break;
case 16:
[self goToWait16];
break;
default:
break;
}}
这是我的代码,用于标记和标记排列成一个圆圈的按钮。
float buttonRadius = 40;
- (void)playerButtons {
[self centreReference]; // get centre of the circle of buttons
for (int i = 1; i < buttons+1; i++) {
UIView *newButton = [[UIView alloc] initWithFrame:CGRectMake(0, 0, buttonRadius, buttonRadius)];
[self newCentre]; // get x and y coordinates for next button
newButton.center = CGPointMake(x,y);
NSLog(@"%i %@", i, NSStringFromCGPoint(newButton.center));
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
aButton.frame = (CGRect) {x, y, buttonRadius, buttonRadius};
aButton.clipsToBounds = YES;
aButton.layer.masksToBounds = NO;
[aButton setBackgroundImage:[UIImage imageNamed:@"Button.png"] forState:UIControlStateNormal];
aButton.titleLabel.textAlignment = NSTextAlignmentCenter;
aButton.titleLabel.font = [UIFont fontWithName: @"HelveticaNeue-UltraLight" size: 20];
aButton.titleLabel.hidden = NO;
aButton.titleLabel.layer.opacity = 1.0f;
[aButton setTitleColor: [UIColor blackColor] forState:UIControlStateNormal];
编辑 2
aButton.tag= i;
[aButton setTitle:[NSString stringWithFormat:@"%i", i] forState:UIControlStateNormal];
NSLog(@"aButton tag=%li", aButton.tag);
[aButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
}}
在我点击按钮之前,我注意到每个标签都记录为一个长符号
例如
-6917529027641081071
-6917529027641081055
-6917529027641081039
-6917529027641081023
-6917529027641081007
-6917529027641080991
-6917529027641080975
-6917529027641080959
-6917529027641080943
-6917529027640880366
-6917529027640872174
-6917529027640868078
-6917529027640859886
谁能解释为什么记录的标签不应该是我设置的 1 到 16 之间的数字?
或者我在点击按钮时检测标签时做错了什么?
编辑 3
for 循环现在生成 1 到 16 范围内的标签
1 {199.799072265625, 187.91653442382812}
aButton tag=1
2 {233.53910827636719, 210.46089172363281}
aButton tag=2
3 {256.08346557617188, 244.200927734375}
aButton tag=3
4 {264, 284}
aButton tag=4
5 {256.08346557617188, 323.799072265625}
aButton tag=5
6 {233.53909301757812, 357.53912353515625}
aButton tag=6
7 {199.799072265625, 380.08346557617188}
aButton tag=7
8 {159.99998474121094, 388}
aButton tag=8
9 {120.20090484619141, 380.08346557617188}
aButton tag=9
10 {86.46087646484375, 357.53909301757812}
aButton tag=10
11 {63.916522979736328, 323.799072265625}
aButton tag=11
12 {56, 284}
aButton tag=12
13 {63.916530609130859, 244.200927734375}
aButton tag=13
14 {86.460891723632812, 210.46090698242188}
aButton tag=14
15 {120.20091247558594, 187.91653442382812}
aButton tag=15
16 {160, 180}
aButton tag=16
更改您的代码:
- (void)buttonTapped:(id)sender forButtonWithTag: (int)tag{
UIButton *tappedButton = (UIButton *)sender;
[tappedButton setTag:tag];
NSLog(@"tapped Button %i", tag);
// tag should select 1 of 16 states
switch (tag) {
到
- (void)buttonTapped:(id)sender{
UIButton *tappedButton = (UIButton *)sender;
[tappedButton setTag: tappedButton.tag];
NSLog(@"tapped Button %i", tappedButton.tag);
// tag should select 1 of 16 states
switch (tappedButton.tag) {
还有这个
[aButton setTag:[NSString stringWithFormat:@"%d", i]];
到
aButton.tag= i;
我正在使用 for 循环来标记和标注 16 个按钮。我正在尝试确定点击了哪个按钮。但是每当我点击一个按钮时,我的应用程序就会终止
due to uncaught exception ’NSInvalidArgumentException’, reason: ‘-[LocalView buttonTapped:]: unrecognized selector sent to instance 0x7ff38a077800’.
Using a Mutable Array has been suggested 作为识别按钮标签的一种方式,但我不明白为什么当 属性 已经定义时有必要这样做。
我需要标签在 1 到 16 范围内,但由于我不明白的原因,NSLog 迫使我使用 %li
而不是 %i
来记录每个标签。
这是我修改后的代码 select 一个按钮(编辑 1)
- (void)buttonTapped:(id)sender{
UIButton *tappedButton = (UIButton *)sender;
// [tappedButton setTag: tappedButton.tag]; EDIT 4
NSLog(@"tapped Button %i", tappedButton.tag);
// tag should select 1 of 16 states
switch (tappedButton.tag) {
case 1:
[self goToWait1];
break;
case 2:
[self goToWait2];
break;
case 3:
[self goToWait3];
break;
case 4:
[self goToWait4];
break;
case 5:
[self goToWait5];
break;
case 6:
[self goToWait6];
break;
case 7:
[self goToWait7];
break;
case 8:
[self goToWait8];
break;
case 9:
[self goToWait9];
break;
case 10:
[self goToWait10];
break;
case 11:
[self goToWait11];
break;
case 12:
[self goToWait12];
break;
case 13:
[self goToWait13];
break;
case 14:
[self goToWait14];
break;
case 15:
[self goToWait15];
break;
case 16:
[self goToWait16];
break;
default:
break;
}}
这是我的代码,用于标记和标记排列成一个圆圈的按钮。
float buttonRadius = 40;
- (void)playerButtons {
[self centreReference]; // get centre of the circle of buttons
for (int i = 1; i < buttons+1; i++) {
UIView *newButton = [[UIView alloc] initWithFrame:CGRectMake(0, 0, buttonRadius, buttonRadius)];
[self newCentre]; // get x and y coordinates for next button
newButton.center = CGPointMake(x,y);
NSLog(@"%i %@", i, NSStringFromCGPoint(newButton.center));
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
aButton.frame = (CGRect) {x, y, buttonRadius, buttonRadius};
aButton.clipsToBounds = YES;
aButton.layer.masksToBounds = NO;
[aButton setBackgroundImage:[UIImage imageNamed:@"Button.png"] forState:UIControlStateNormal];
aButton.titleLabel.textAlignment = NSTextAlignmentCenter;
aButton.titleLabel.font = [UIFont fontWithName: @"HelveticaNeue-UltraLight" size: 20];
aButton.titleLabel.hidden = NO;
aButton.titleLabel.layer.opacity = 1.0f;
[aButton setTitleColor: [UIColor blackColor] forState:UIControlStateNormal];
编辑 2
aButton.tag= i;
[aButton setTitle:[NSString stringWithFormat:@"%i", i] forState:UIControlStateNormal];
NSLog(@"aButton tag=%li", aButton.tag);
[aButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
}}
在我点击按钮之前,我注意到每个标签都记录为一个长符号 例如
-6917529027641081071
-6917529027641081055
-6917529027641081039
-6917529027641081023
-6917529027641081007
-6917529027641080991
-6917529027641080975
-6917529027641080959
-6917529027641080943
-6917529027640880366
-6917529027640872174
-6917529027640868078
-6917529027640859886
谁能解释为什么记录的标签不应该是我设置的 1 到 16 之间的数字?
或者我在点击按钮时检测标签时做错了什么?
编辑 3
for 循环现在生成 1 到 16 范围内的标签
1 {199.799072265625, 187.91653442382812}
aButton tag=1
2 {233.53910827636719, 210.46089172363281}
aButton tag=2
3 {256.08346557617188, 244.200927734375}
aButton tag=3
4 {264, 284}
aButton tag=4
5 {256.08346557617188, 323.799072265625}
aButton tag=5
6 {233.53909301757812, 357.53912353515625}
aButton tag=6
7 {199.799072265625, 380.08346557617188}
aButton tag=7
8 {159.99998474121094, 388}
aButton tag=8
9 {120.20090484619141, 380.08346557617188}
aButton tag=9
10 {86.46087646484375, 357.53909301757812}
aButton tag=10
11 {63.916522979736328, 323.799072265625}
aButton tag=11
12 {56, 284}
aButton tag=12
13 {63.916530609130859, 244.200927734375}
aButton tag=13
14 {86.460891723632812, 210.46090698242188}
aButton tag=14
15 {120.20091247558594, 187.91653442382812}
aButton tag=15
16 {160, 180}
aButton tag=16
更改您的代码:
- (void)buttonTapped:(id)sender forButtonWithTag: (int)tag{
UIButton *tappedButton = (UIButton *)sender;
[tappedButton setTag:tag];
NSLog(@"tapped Button %i", tag);
// tag should select 1 of 16 states
switch (tag) {
到
- (void)buttonTapped:(id)sender{
UIButton *tappedButton = (UIButton *)sender;
[tappedButton setTag: tappedButton.tag];
NSLog(@"tapped Button %i", tappedButton.tag);
// tag should select 1 of 16 states
switch (tappedButton.tag) {
还有这个
[aButton setTag:[NSString stringWithFormat:@"%d", i]];
到
aButton.tag= i;