如何识别以编程方式创建的按钮标记

时间:2016-03-20 09:29:58

标签: ios objective-c for-loop button sender

我使用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来记录每个标签。

这是我修改后的代码选择按钮(编辑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

1 个答案:

答案 0 :(得分:0)

更改您的代码:

- (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;