Pergunta

I am a new iPad developer.

I have created UIButton programmatically, in which I want to identify user has clicked on which button and according to that I want to do some action.

How should I identify this?

Here is my code snippet:

for(int i=0;i<[_array count];i++)
    {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.tag=count;
        [button addTarget:self 
                   action:@selector(aMethod:)
         forControlEvents:UIControlEventTouchDown];

        button.backgroundColor=[UIColor redColor];
        [button setTitle:@"Show View" forState:UIControlStateNormal];
        button.frame = CGRectMake(xpos, 270.0, 200.0, 150.0);

        [self.view addSubview:button];            
    }

This is what I'm doing now: I thought I will assign count to each button and I will pass this count to button click method. Is this a good idea? Is there any other way possible?

On button click I'm calling aMethod:

-(void)aMethod:(id)sender{
    NSLog(@"btn clicked");

}

Any help will be appreciated!

Foi útil?

Solução

-(void)aMethod:(UIButton*)sender{
    NSLog(@"btn clicked %d", sender.tag);

}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top