2010-09-17 61 views
0

您好我是iphone新手。我所做的是在視圖中創建一組按鈕。我的代碼是作爲休耕地如何從一組按鈕中獲取所有按鈕標記值

- (void)mymethod1 { 
    UIView *view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; 
     int row = 0; 
     int column = 0; 

     for (int i=6; i<_images.count; i++) { 

      buttonimage = [UIButton buttonWithType:UIButtonTypeCustom]; 

      buttonimage.frame = CGRectMake(column*60+5, row*60+5, 70,70); 
      [buttonimage setImage:[UIImage imageNamed:[_images objectAtIndex:i]] forState:UIControlStateNormal]; 
      buttonimage.tag = i; 
      [buttonimage addTarget:self 
         action:@selector(buttonClicked:) 
      forControlEvents:UIControlEventTouchUpInside]; 
      buttonimage.tag = i; 

      [view addSubview:buttonimage]; 

      if (column == 4) { 
       column = 0; 
       row++; 
      } else { 
       column++; 
      } 

     } 

     self.view = view; 
     [ view release]; 
} 

它工作正常。但是當過我所說的所有的按鈕在任何其他功能它得到最後一個按鈕僅適用於如:

- (void)mymethod2 { 
NSLog(@"button value %d",buttonimage.tag); 

} 

「得到它在控制檯按鈕值5」。但我需要得到所有mymethod2按鈕標籤值。我怎麼能得到所有的按鈕標籤值。請張貼一些code.Thank你提前。

回答

6

您需要遍歷當前視圖的所有子視圖,然後檢查,看它是否是在澆鑄前的按鈕,像這樣

for (UIView* view in [self.view subviews]) { 
    if([view isKindOfClass:[UIButton class]]){ 
    UIButton *btn = (UIButton*)view; 
    NSLog(@"button value %d", btn.tag); 
    } 
} 
0

所有按鈕標記值: -

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; 
} 
0
UIButton *BtnTmp=sender; 
if([BtnTmp tag]) 
    [BtnTmp setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal]; 
else 
    [BtnTmp setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal]; 

BtnTmp.tag=!BtnTmp.tag; 
相關問題