Nadhas
Nadhas

Reputation: 5797

how to creating dynamic buttons iphone xcode?

I want to create uibuttons dynamically. so i for loop to create button with tag and added to view. all buttons performs the same action with respect to its tag value... but i want to change the propery of the button. Therefore i need to get the uibutton using the tag value...

my code...

UIButton *button2;
//view did load
int width = 30;

for(int i = 1; i <= 5; i++)
{
    button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 addTarget:self action:@selector(ratingAction:) forControlEvents:UIControlEventTouchUpInside];
    [button2 setBackgroundImage:[UIImage imageNamed:@"star1.png"] forState:UIControlStateNormal];
    button2.tag = i;
    button2.backgroundColor = [UIColor clearColor];
    button2.frame = CGRectMake(width, 78, 15, 15);

    [self.view addSubview:button2];

    width = width +30;
}

-(void)ratingAction:(id*)sender
{
    // here using the tag value i want to get the button and change the background image….
    // for example i want to change the background for tag values 1,3,6,7 ,8…
}

Upvotes: 0

Views: 14966

Answers (3)

-(void)ratingAction:(id*)sender
{
    // here using the tag value i want to get the button and change the background image….
    //  for example i want to change the background for tag values 1,3,6,7 ,8…      
    if ([sender isKindOfClass:[UIButton class]])
    {
        UIButton *temp=(UIButton*)sender;

        if ([temp tag]==1 || [temp tag]==3 || [temp tag]==6 || [temp tag]==7 )
        {
            [temp setBackgroundColor:[UIColor redColor]];
        }
    }
}

Upvotes: 1

Sri
Sri

Reputation: 51

Check this code

    - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    int y = 100;
    for (int i=0; i<=5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button addTarget:self 
                   action:@selector(aMethod:)
         forControlEvents:UIControlEventTouchDown];
        [button setTitle:@"Show View" forState:UIControlStateNormal];
        button.frame = CGRectMake(80, y, 160, 40);
        [button setTitle:[NSString stringWithFormat:@"Button-%i", i] forState:UIControlStateNormal];
//        [button set

        [self.view addSubview:button];
        y=y+60;
    }
}

-(void)aMethod:(id)sender{
    UIButton *button = sender;
    int y = 100;
    int i = 5;

    for (int x=0; x<=i; x++) {
        NSString *frameString = [NSString stringWithFormat:@"{{80, %i}, {160, 40}}", y];
        if ([NSStringFromCGRect(button.frame) isEqualToString:frameString])  {
            NSLog(@"button %i clicked..", x);
        }
    y= y+60;
    } 
}

Upvotes: 0

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

Use viewWithTag function of UIView to access your UIButton using the tag value.

See in Documentation viewWithTag

Use it as below.

UIButton* myButton = (UIButton*)[mySuperView viewWithTag:1];
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:3];
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:6];
.........

Upvotes: 1

Related Questions