Illep
Illep

Reputation: 16849

Connecting Buttons which are added using interface Builder using code - Beginner

I have created 4 buttons via Interface Builder. I have an array which has the following content stored in it

"A"
"B"
"B"
"A"

Now what i need to do is to read the array of strings and paint the button Yellow or Red. If the String is A then the button should be Yellow, and if the String character is B the button should be Red.

My code so far;

for (NSString* content in arr) {
           if ([content isEqualToString:@"A"]){
               // Make the (1st/there after) button in the interface builder to Yellow and etc 
           }else {
// Make the 1st button in the interface builder to Redand etc
}
       }

Upvotes: 0

Views: 267

Answers (3)

Sam
Sam

Reputation: 2579

You should tag the buttons in IB, or create Outlets for them in the subclass. Then you can reference them by these tags or outlets.

Once you have created all of the outlets, you could store the references in a member variable containing the outlets. Then it is a simple matter of setting the background color to the correct value based on the string.

Upvotes: 1

demon9733
demon9733

Reputation: 854

Actually, you can use tag property of UIButton. Suppose, your button tags will be 1, 2, 3, 4. So:

NSArray *array = [NSArray arrayWithObjects:@"A", @"B", @"B", @"A", nil];
for (int i = 0; i < 4; i++) {
    UIView *view = [self.view viewWithTag:i + 1];
    if ([[array objectAtIndex:i] isEqualToString:@"A"])
        view.backgroundColor = [UIColor yellowColor];
    else if ([[array objectAtIndex:i] isEqualToString:@"B"])
        view.backgroundColor = [UIColor redColor];
}

Besides, I wouldn't recommend you to use IB, especially at the beginning. Write the code by yourself. This code may contain syntax or logic errors (I wrote it in notepad):

@interface ViewController : UIViewController
@end

//

@implementation ViewController

- (void)loadView {
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    self.view.backgroundColor = [UIColor whiteColor];

    NSArray *array = [NSArray arrayWithObjects:@"A", @"B", @"B", @"A", nil];
    CGFloat offset = 5.0f, step = self.view.frame.size.width / [array count];
    for (NSString *string in array) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(offset, 5, step - 10, 50);
        offset += step;

        if ([string isEqualToString:@"A"])
            button.backgroundColor = [UIColor yellowColor];
        else if ([string isEqualToString:@"B"])
            button.backgroundColor = [UIColor redColor];

        [self.view addSubview:button];
    }
}

@end

Upvotes: 0

lnafziger
lnafziger

Reputation: 25740

First, in Interface Builder (IB) you need to assign tag's to each button that you want to change so that you can retrieve them later. Set the first button tag to 0, second to 1, etc.

Then your code would look something like this:

for (NSUInteger i = 0; i < 4; i++) {
    UIButton *button = (UIButton *)[self.view viewWithTag:i];
    if ([(NSString *)[arr objectAtIndex:i] isEqualToString:@"A"]) {
        [button setBackgroundColor:[UIColor yellowColor]];
    } else {
        [button setBackgroundColor:[UIColor redColor]];
    }
}

Upvotes: 1

Related Questions