【问题标题】:How to use Tag on dynamic button in IOS?如何在IOS中的动态按钮上使用标签?
【发布时间】:2013-12-05 23:08:19
【问题描述】:

我使用 for 循环创建了按钮。按钮做得非常好。现在我必须对所有动态按钮进行单一的 IB 操作。我也设置了标签。我正在使用的代码如下所示。但该操作并未确定我要使用的确切按钮。

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

     btnphoto=[[UIButton alloc]initWithFrame:CGRectMake(10,(30*i)+110,50,20)];

     [btnphoto setTitle:@"Photo" forState:UIControlStateNormal];

     [btnphoto addTarget:self action:@selector(someMethod:)forControlEvents:UIControlEventTouchUpInside];

     btnphoto.tag=100 + i;

     [self.view addSubview:btnphoto];

}

-(void)someMethod:(UIButton *)sender{

}

【问题讨论】:

    标签: ios iphone ipad tags ios7


    【解决方案1】:

    试试上面的代码,这可能会有所帮助:

    -(void)someMethod:(id)sender{
       UIButton *btn = (UIButton *)[self.view viewWithTag:btn_tag];
        switch([btn tag])
        {
         case :101
           [btn setBackgroundColor:[UIColor redColor]];
            break;
    
          case :102
               [btn setBackgroundColor:[UIColor redColor]];
               break;
    
          case :103
               [btn setBackgroundColor:[UIColor blueColor]];
               break;
       }
    }
    

    【讨论】:

      【解决方案2】:

      由于您已经为每个按钮设置了标签,因此只需执行以下操作即可识别已触摸的按钮

      -(void)someMethod:(id)sender
      {
          UIButton *TouchedButton=(UIButton *)sender;
          int TouchedButtonTag=[TouchedButton tag]; // Now you can uniquely identify your buttons by this tag. 
          // do your stuff for respective button touched event
      
         switch([TouchedButton tag])
         {
            case :101
                 [TouchedButton setBackgroundColor:[UIColor redColor]];
                 break;
      
            case :102
                 [TouchedButton setBackgroundColor:[UIColor redColor]];
                 break;
      
            case :103
                 [TouchedButton setBackgroundColor:[UIColor blueColor]];
                 break;
      
           // and so on...
         }
      }
      

      【讨论】:

      • 您好小吃。例如,如果我想说更改按钮的颜色。并且在每个按钮上我必须执行相同的操作。即每个按钮都应该执行相同的操作。那么我该如何实现那个。
      【解决方案3】:

      试试这个

      for (int i=0; i < 10; i++){
      
          UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(10,(30*i)+110,50,20)];
      
          [button setTitle:@"Photo" forState:UIControlStateNormal];
      
          [button addTarget:self action:@selector(someMethod:)forControlEvents:UIControlEventTouchUpInside];
      
          button.tag=100 + i;
      
          [self.view addSubview:button];
      
      }
      

      要显示不同的颜色,首先在数组中添加颜色

      NSMutableArray *colorArray=[[NSMutableArray alloc] initWithObjects:[UIColor redColor],[UIColor blueColor],[UIColor greenColor],nil];    
      

      然后

      -(void)someMethod:(UIButton *)sender{
          NSLog(@"Btn Tag = %d",sender.tag);
          int randomNum=arc4random()%3;
          [sender setBackgroundColor:[colorArray objectAtIndex:randomNum]];
      }
      

      【讨论】:

      • 您好 Rajneesh,谢谢您的回答..我使用此代码..现在每个按钮上的颜色为红色。但如果我希望第一个按钮有红色,第二个有蓝色,第三个有否则我该怎么做。
      【解决方案4】:

      您必须从函数中实际访问按钮的 tag 属性以确定哪个按钮调用了选择器。

      -(void)someMethod:(UIButton *)sender {
      
          UIButton *tappedButton = sender;
      
          NSInteger tag = tappedButton.tag;
          NSLog(@"This is the button with tag: %ld",tag);
      
          [tappedButton setBackgroundColor:[UIColor redColor]];
      }
      

      【讨论】:

      • 是的,来自这段代码//现在我已经确定我点击了哪个按钮。机器人举例说,如果在每个按钮上点击。我想改变按钮的颜色。你能告诉那我该怎么做。提前谢谢
      【解决方案5】:

      如果你想改变颜色或任何属性,那么你可以这样做

      //我只是给你一个例子,我如何改变 UIButton 的标题,就像你可以改变颜色属性一样......我刚刚在我的一些代码中使用了这个......

       -(void)someMethod:(id)sender {
                //1...this will change all the button title with string "Changed" 
                  [sender setTitle:@"Changed" forState:UIControlStateNormal];
      
      
               //2..now if you want to just change the property of uibutton on basis of tag value
                   [(UIButton *)[self.contentView viewWithTag:Your_TAG_VALUE] setTitle:@"JustChanged" forState:UIControlStateNormal];
      
      
          }
      

      【讨论】:

        【解决方案6】:
        -(void)someMethod:(id)sender{     
        
               UIButton *btn = (UIButton *)sender;
               int *btn_tag=btn.tag;
        
               NSLog(@"This is the button with tag: %d", btn_tag);
        
        
            }
        

        【讨论】:

        • 你好 OmPrakash。例如,我想说改变按钮的颜色。在每个按钮上我必须执行相同的操作。即每个按钮都应该执行相同的操作。那么我该如何实现.
        • UIButton *btn = (UIButton *)[self.view viewWithTag:btn_tag]; [btn setBackgroundColor:color];
        【解决方案7】:

        试试这个 -

        在 someMethod 方法中 -

        UIButton *button = sender;
        
        int buttonTagIndex = button.tag;
        

        现在 buttonTagIndex 为您提供所选按钮的标签索引。

        【讨论】:

        • 你好 Smita。谢谢你的回答,如果我想说改变按钮的颜色,例如,我必须在每个按钮上执行相同的操作。即每个按钮都应该执行相同的操作.那我怎么能做到这一点。
        • 我没有正确理解你。如果您想对每个按钮操作执行相同的操作,则无需获取按钮标签。但如果你想改变按钮颜色,那么你可以。
        • 你说得对...对不起我错了..但是如果我必须在每个按钮操作上更改不同的颜色..那我该怎么做
        • @user2754190 请看我的回答...它会为您提供帮助...如果有任何疑问,请询问
        猜你喜欢
        • 2012-11-22
        • 1970-01-01
        • 1970-01-01
        • 2014-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多