【问题标题】:How to get tag number of UIButton如何获取 UIButton 的标签号
【发布时间】:2011-10-10 04:26:03
【问题描述】:

我正在使用各种按钮,并且我已经从 Xib 文件中设置了它的标签。并将所有按钮连接到单个方法-(void) note:(id)sender

现在我想检索标签号。这样我就可以看到点击了哪个按钮

-(void) note:(id)sender

{

    NotesClass *note = [[NotesClass alloc] initWithNibName:@"NotesClass" bundle:nil];
    note.notetag = sender;
    NSLog(@"%@",note.notetag);
    [self.navigationController pushViewController:note animated:YES];

}

当打印那个 NSlog 我得到这个输出:

<UIButton: 0x4e70350; frame = (227 119; 20 18); opaque = NO; autoresize = RM+BM; tag = 1; layer = <CALayer: 0x4e70480>>

任何人都可以帮助我。

【问题讨论】:

    标签: iphone objective-c ios xib nib


    【解决方案1】:

    你可以用

    获取标签
    sender.tag
    

    【讨论】:

    • 我试过了,但它显示错误。出现以下错误“请求非结构或联合中的成员‘标签’”
    • 你是在 NSLog 中打印吗? tag 是一个整数属性。您必须以“%d”作为打印整数的格式。
    【解决方案2】:
    (void) note:(id)sender
    
    {
    
        NotesClass *note = [[NotesClass alloc] initWithNibName:@"NotesClass" bundle:nil];
        note.notetag = [sender tag];
        NSLog(@"%d",note.notetag);
        [self.navigationController pushViewController:note animated:YES];
    
    }
    

    【讨论】:

      【解决方案3】:
      -(void) note:(id)sender
      
      {
      
      NotesClass *note = [[NotesClass alloc] initWithNibName:@"NotesClass" bundle:nil];
      note.notetag = [sender tag];
      NSLog(@"%d",note.notetag);
      //Another option is to use
      
      UIButton *button = (UIButton *)sender;
      NSLog(@"%d",button.tag);
      [self.navigationController pushViewController:note animated:YES];
      
      }
      

      它的%d 不是%@ 因为tagint 类型

      【讨论】:

        【解决方案4】:

        试试下面的代码,一定能帮到你

        UIButton *button = (UIButton *)sender;
            NSInteger bTag = button.tag;
        

        【讨论】:

          【解决方案5】:
          in .H file write below code
          
          @interface tagViewController : UIViewController {
          
              UIButton *btn1;
          }
          @property(nonatomic,retain)IBOutlet UIButton *btn1;
          -(IBAction)btnclicked:(id)sender;
          @end
          
          and in .M file write below code
          
          -(IBAction)btnclicked:(id)sender
          {
              btn1 = (UIButton *)sender;
          
              NSLog(@"You Press Button No %d",btn1.tag);
          
          }
          
          Don't forgate maping of your button Suppose i have three button and i set it tag 1,2,3 and then after mapping all of them with  btnclicked: in TouchUp Inside Event and then after run it it's working...
          

          【讨论】:

            猜你喜欢
            • 2013-07-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-09-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多