簡體   English   中英

iPhone中的動態UIButton控件和動作?

[英]Dynamic UIButton controlls and actions in iphone?

我正在創建一個包含動態頁面的應用程序。 實際上,我通過檢查服務器的響應來創建一些動態問題和UIButtons(自定義復選框)。 我的代碼在這里,

  NSString *response=[ResponseFromServer ObjectAtIndex:0];


 if ([comSt compare: @"CheckBoxList"]==NSOrderedSame)
            {

                int count=[ResponseFromServer count];

                            for(int i=0;i<count;i++)
                            {


                                button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                                button.tag=i;
                                [button setFrame:CGRectMake(0.0f, 0.0f, 37, 37)];
                                [button setCenter:CGPointMake(116.0,p1)];
                                [button setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];

                                [button setBackgroundColor:[UIColor whiteColor]];
                                [button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
                                [self.scrollView addSubview:button];

                                p1=p1+30;
                            }

-(void)checkButtonTapped:(id)sender
    {

      int tagVal = [(UIButton*)sender tag]; 


         if (tagVal==0) {
              NSLog(@"tag =  0");

             [button setSelected:YES];
             [button setImage:[UIImage imageNamed:@"check.png" forState:UIControlStateSelected];  //not working, button image is not changing 



      }

假設我創建了5個帶有不同標簽的動態按鈕。 我首先將所有這些按鈕的圖像設置為uncheck.png。 有用。 但是當我單擊一個特定的按鈕並且需要將該按鈕圖像更改為checked.png時,它在按鈕操作上不起作用。 我需要像checkBox控件一樣更改它,如何從一組按鈕訪問動態創建的按鈕的控件?

我想你能理解我的要求,在此先感謝。

在您的代碼中,您需要像這樣進行更改

試試這個代碼

 if ([comSt compare: @"CheckBoxList"]==NSOrderedSame)
    {
        int count=[ResponseFromServer count];

        for(int i=0;i<count;i++)
        {
            button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.tag=0; 
            [button setFrame:CGRectMake(0.0f, 0.0f, 37, 37)];
            [button setCenter:CGPointMake(116.0,p1)];
            [button setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];

            [button setBackgroundColor:[UIColor whiteColor]];
            [button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
            [self.scrollView addSubview:button];

            p1=p1+30;
        }
    }
-(void)checkButtonTapped:(id)sender
    {
        int tagVal = [(UIButton*)sender tag]; 

        if (tagVal==0) {
            NSLog(@"tag =  0");

            [button setSelected:YES];
            [button setImage:[UIImage imageNamed:@"check.png" forState:UIControlStateSelected];   

             [button setTag:100];

             }

             else
             {

                 [button setSelected:YES];
                 [button setImage:[UIImage imageNamed:@"uncheck.png" forState:UIControlStateSelected];   

                  [button setTag:0];
                  }
 } 

在添加動態按鈕集的同時[button setSelected:NO];

替換方法並添加以下給出的一種方法

  -(void)checkButtonTapped:(id)sender
{

  UIButton *curButton = (UIButton*)sender 

  if (curButton.tag==0) 
  {
     [self buttonAction:curButton];
  }

  if (curButton.tag==1) 
  {
      [self buttonAction:curButton];
  }

  // continues
 }


 -(void)buttonAction:(UIButton *)curButton
 {

   if(![curButton isSelected])
     {
       [curButton setSelected:YES];
       [curButton setImage:[UIImage imageNamed:@"check.png" forState:UIControlStateSelected];  //not working, button image is not changing 
     } else {
       [curButton setSelected:NO];
       [curButton setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];

     }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM