What is the weirdest event when UIButton @selector detected the right button, making the wrong "else_if"?

So, I dynamically create 3 UIButtons (for now), with this loop:

NSMutableArray *sites = [[NSMutableArray alloc] init];

     NSString *one = @"Constution Center";
     NSString *two = @"Franklin Court";
     NSString *three = @"Presidents House";

     [sites addObject: one];
     [one release];

     [sites addObject: two];
     [two release];

     [sites addObject: three];
     [three release];

     NSString *element;
     int j = 0;
     for (element in sites)
     {
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

         //setframe (where on screen)
         //separation is 15px past the width (45-30)
         button.frame = CGRectMake(a, b + (j*45), c, d);

         [button setTitle:element forState:UIControlStateNormal];

         button.backgroundColor = [SiteOneController myColor1];

        [button addTarget:self action:@selector(showCCView:)
        forControlEvents:UIControlEventTouchUpInside];
         [button setTag:j];

         [self.view addSubview: button];
         j++;
     }

The @Selector method is here:

- (void) showCCView:(id) sender {

    UIButton *button = (UIButton *)sender;
    int whichButton = button.tag;
    NSString* myNewString = [NSString stringWithFormat:@"%d", whichButton];
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    self.view.backgroundColor = [UIColor whiteColor];

    UINavigationBar *cc = [SiteOneController myNavBar1:@"Constitution Center Content"];
    UINavigationBar *fc = [SiteOneController myNavBar1:@"Franklin Court Content"];
    UINavigationBar *ph = [SiteOneController myNavBar1:@"Presidents House Content"];

    if (whichButton = 0) 
    {
        NSLog(myNewString);
        [self.view addSubview:cc];
    }
    else if (whichButton = 1) 
    {
        NSLog(myNewString);
        [self.view addSubview:fc];
    }
    else if (whichButton = 2) 
    {
        NSLog(myNewString);
        [self.view addSubview:ph];
    }
}

Now it prints the correct button tag in NSLog, as shown in the method, however EACH SINGLE BUTTON displays a navigation bar with "Franklin Court" as the name of EVERY ONE, even if when you press the 0 button, it says "Button 0 is pressed" in the console. but still executes the else if ((whichButton = 1) code.

Did I miss something?

+3
source share
2 answers

= == , .

, whichButton 0, false, whichButton 1, true, , -.

+7

, == =.

:

    if (whichButton = 0) 
    {
        NSLog(myNewString);
        [self.view addSubview:cc];
    }
    ...

:

    if (whichButton == 0) 
    {
        NSLog(myNewString);
        [self.view addSubview:cc];
    }
    else if (whichButton == 1) 
    {
        NSLog(myNewString);
        [self.view addSubview:fc];
    }
    else if (whichButton == 2) 
    {
        NSLog(myNewString);
        [self.view addSubview:ph];
    }

:


NSLog (myNewString); //occurs in all cases.

switch (whichButton)
{
    case 0:
        [self.view addSubview:cc];
        break;
    case 1:
        [self.view addSubview:fc];
        break;
    case 2:
        [self.view addSubview:ph];
        break;
    default:
        // optionally handle the case where the button tag was not 0, 1, or 2.
}
+2

Source: https://habr.com/ru/post/1736587/


All Articles