I am using a for loop to tag and label 16 buttons. I am trying to identify which button is tapped. But whenever I tap a button my app terminates
due to uncaught exception ’NSInvalidArgumentException’, reason: ‘-[LocalView buttonTapped:]: unrecognized selector sent to instance 0x7ff38a077800’.
Using a Mutable Array has been suggested as a way to identify button tags but I can't see why that would be necessary when the property is already defined.
I need tags to be in the range 1 to 16 but for reasons I don't understand NSLog has forced me to use %li
instead of %i
to log each tag.
Here is my revised code to select a button (EDIT 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;
}}
And here is my code to tag and label buttons which are arranged in a circle.
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];
EDIT 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];
}}
Before I tap a button I notice that each tag is logged as a long signed number e.g.
-6917529027641081071
-6917529027641081055
-6917529027641081039
-6917529027641081023
-6917529027641081007
-6917529027641080991
-6917529027641080975
-6917529027641080959
-6917529027641080943
-6917529027640880366
-6917529027640872174
-6917529027640868078
-6917529027640859886
Can anyone explain why the tags logged should not be numbers between 1 and 16 which I set ?
Or am doing I something wrong to detect a tag when a button is tapped ?
EDIT 3
The for loop now produces tags in the range 1 to 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
change your code:
- (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) {
to
- (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) {
and this
[aButton setTag:[NSString stringWithFormat:@"%d", i]];
to
aButton.tag= i;