2010-09-16 80 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; 

而使用它,你必須與帕旺回答。

快樂編碼...