Mahesh Babu
Mahesh Babu

Reputation: 3433

how can i create array of buttons

hi i am new to iPhone.what i did is creating a 4 buttons individually.i need button tag values.when ever i check it in console i got 0 for 4 buttons,because i create 4 individual buttons .But i need buttons tag values like for first button, tag value 0,for second button, tag value 1.... like this how can i done this pls post some code.Thank u in advance.

Upvotes: 1

Views: 1742

Answers (3)

Suresh Varma
Suresh Varma

Reputation: 9740

By default the tag of the button is zero So if you have created four individual buttons the tag for all will be zero So What you can do is If you have added four buttons from the xib file then set their tag as per your requirement in the xib file itself and give them same name If you have taken the four buttons through code then set the tag through code

  //Alloc buttonName1
    buttonName1.tag=0;
    //Alloc buttonName1
    buttonName1.tag=1;
 //Alloc buttonName1
    buttonName1.tag=2;
 //Alloc buttonName1
    buttonName1.tag=3;

And for using it you have to go with pawans answer.

hAPPY cODING...

Upvotes: 0

iOSPawan
iOSPawan

Reputation: 2894

for(int i=0;i<3;i++){
UIButton *theButton=[[UIButton alloc]init];
theButton.tag=i;
//set their selector using add selector
[theButton addTarget:self action:@selector(buttonClicked:) 
forControlEvents:UIControlEventTouchDown];
//set their frame color title or image
}

-(void)buttonClicked:(UIButton *)inButton{
int tags=inbutton.tag;
}

Upvotes: 1

Vivi
Vivi

Reputation: 972

You can specify the button tag like this :

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTag:1];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setTag:2];

Upvotes: 1

Related Questions