Q:

How to use Tag on dynamic button in IOS?

I have created buttons by using for loop. The buttons are made perfectly fine. Now I have to single IB action for all of the dynamic buttons. I have set tag too.The code I am using is below written. But the action is not identifying the exact button which I want to use.
for (int i=0; i < 10; i++){

     btnphoto=[[UIButton alloc]initWithFrame:CGRectMake(10,(30*i)+110,50,20)];

     [btnphoto setTitle:@"Photo" forState:UIControlStateNormal];

     [btnphoto addTarget:self action:@selector(someMethod:)forControlEvents:UIControlEventTouchUpInside];

     btnphoto.tag=100 + i;

     [self.view addSubview:btnphoto];

}

-(void)someMethod:(UIButton *)sender{

}

A:

Try this - 
In someMethod method - 
UIButton *button = sender;

int buttonTagIndex = button.tag;

Now buttonTagIndex gives you tag index for selected button.

A:

Try this 
for (int i=0; i < 10; i++){

    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(10,(30*i)+110,50,20)];

    [button setTitle:@"Photo" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(someMethod:)forControlEvents:UIControlEventTouchUpInside];

    button.tag=100 + i;

    [self.view addSubview:button];

}

To show diffrent color, first add color in array
NSMutableArray *colorArray=[[NSMutableArray alloc] initWithObjects:[UIColor redColor],[UIColor blueColor],[UIColor greenColor],nil];    

then
-(void)someMethod:(UIButton *)sender{
    NSLog(@"Btn Tag = %d",sender.tag);
    int randomNum=arc4random()%3;
    [sender setBackgroundColor:[colorArray objectAtIndex:randomNum]];
}

