开发者

how can i create array of buttons

开发者 https://www.devze.com 2023-01-16 15:05 出处:网络
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 b

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 t开发者_开发百科his pls post some code.Thank u in advance.


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;
}


You can specify the button tag like this :

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

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


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号