2010-09-16 76 views
1

嗨,我是新来的iPhone.what我做的是创建一个4个按钮individual.i需要按钮标记values.when我检查它在控制台我得到0为4按钮,因为我创建4个单独的按钮。但我需要按钮标签值,如第一个按钮,标签值0,第二个按钮,标签值1 ....这样我怎么能做到这一点,请张贴一些code.Thank你提前。如何创建按钮阵列

回答

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

您可以指定按钮标签是这样的:

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

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

你可以这样做,因为UIButton类继承自UIView类。 – Vivi 2010-09-16 09:51:55

0

默认情况下,该按钮的标签是零所以,如果你已经创建了四个单独的按钮,所有的标签将是零,那么你可以做的是 如果您已经添加了xib文件中的四个按钮,然后根据您的要求在xib文件本身中设置它们的标签,并为它们指定相同的名称 如果您已通过代码拍摄了四个按钮,然后通过代码设置标签

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

而使用它,你必须与帕旺回答。

快乐编码...