views:

264

answers:

2

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 is printing the correct button tag to NSLog, as shown in the method, however EVERY SINGLE BUTTON is displaying a navigation bar with "Franklin Court" as the title, EVERY SINGLE ONE, even though when I click button 0, it says "Button 0 clicked" in the console, but still performs the else if (whichButton = 1) code.

Am I missing something here?

+7  A: 

You're using the = operator instead of the == operator in your conditions, which makes an assignment instead of a comparison.

Since the returned value of an assignment is the assigned value, first whichButton becomes 0 and that evaluates to false, and then whichButton becomes 1 and that evaluates to true, and no matter what you do you end up with the Franklin Court option.

zneak
This is the reason I promote the coding style if(0 == whichButton), because the compiler will catch the error
sbooth
@sbooth: this is the reason I promote languages that only accept a boolean expression in their branching structures.
zneak
The problem with `constant == variable` is that it doesn't read very well. It's like saying "If blue is the sky" or "If tall is the man". The problem is that the I think the default compiler settings for Xcode disable the assignment warning because of the `if (self = [super init])` idiom.
dreamlax
@dreamlax: This is so right. We should call that comparison style "Yoda Conditions".
zneak
@zsneak: I'm in favour of that.
dreamlax
assignment, not assignation. An assignation is like a tryst.
Jason Coco
@zneak funny, that is
sbooth
@Jason Coco: sorry, I missed your comment when you posted it. Thanks for telling anyways, English isn't my first language so I mess up once in a while (read "very often").
zneak
+2  A: 

You have a problem where you should have used == instead of =.

Instead of this:

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

Try this:

    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];
    }

Or this:


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's tag was not 0, 1, or 2.
}
dreamlax