Ich verwende eine for-Schleife, um 16 Schaltflächen zu markieren und zu beschriften. Ich versuche herauszufinden, welcher Button angetippt wurde. Aber immer, wenn ich tippen Sie auf eine Schaltfläche meine app beendetSo identifizieren Sie programmatisch erstellte Schaltflächen-Tags
due to uncaught exception ’NSInvalidArgumentException’, reason: ‘-[LocalView buttonTapped:]: unrecognized selector sent to instance 0x7ff38a077800’.
Using a Mutable Array has been suggested als Wege-Taste, um Tags zu identifizieren, aber ich kann nicht sehen, warum das notwendig wäre, wenn die Eigenschaft ist bereits definiert.
Ich brauche Tags im Bereich von 1 bis 16, aber aus Gründen, die ich nicht verstehe NSLog hat mich gezwungen, %li
statt %i
zu verwenden, um jedes Tag zu protokollieren.
Hier ist mein überarbeiteter Code eine Taste zur Auswahl (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;
}}
Und hier ist mein Code und Label-Tasten zu markieren, die in einem Kreis angeordnet sind.
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];
}}
Bevor ich auf einen Knopf tippen merke ich, dass jedes Etikett als langvorzeichenbehaftete Zahl angemeldet ist z.B.
-6917529027641081071
-6917529027641081055
-6917529027641081039
-6917529027641081023
-6917529027641081007
-6917529027641080991
-6917529027641080975
-6917529027641080959
-6917529027641080943
-6917529027640880366
-6917529027640872174
-6917529027640868078
-6917529027640859886
Kann jemand erklären, warum die geloggten Marken nicht Zahlen zwischen 1 und 16 sein sollten, die ich einstelle?
Oder mache ich etwas falsch, um ein Tag zu erkennen, wenn eine Taste angetippt wird?
EDIT 3
Die for-Schleife erzeugt jetzt Tags im Bereich von 1 bis 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
@ Анатолий Попов Vielen Dank. Das hat es fast repariert. 'aButton.tag = ich;' '[aButton setTitle: [NSString stringWithFormat: @ "% i", i] forState: UIControlStateNormal]; Also kann ich dies als die akzeptierte Antwort ankreuzen können Sie bitte erklären warum NSLog (@ "aButton-Tag =% i", aButton.tag); ' fragt immer noch, ob Sie eine explizite Umwandlung zu "long" hinzufügen möchten? – Greg
@ Анатолий Попов. Dies ist komplett behoben einschließlich des NSLog (siehe Edit 3). Viele Arbeitsstunden in Minuten festgelegt. Danke noch einmal. – Greg
Ist '[getappedButton setTag: tappedButton.tag];' erforderlich? Sieht aus wie no-op –