【问题标题】:How to get the information about the sender in the case here如何在此处获取案例中的发件人信息
【发布时间】:2012-04-01 16:04:52
【问题描述】:

我正在循环中创建UIButtons,如下所示。

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

        if(i ==1){
            UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)];
            [btn1 setImage:image2 forState:UIControlStateNormal];
            [self addSubview:btn1];
            continue;
        }
        x = x + 40;
        y = y + 50; 
         UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(x , y, 40, 40)];
        [btn2 setImage:image1 forState:UIControlStateNormal];
        [btn2 addTarget:self action:@selector(buttonPressed) 
       forControlEvents:UIControlEventTouchUpInside];

         [self addSubview:btn2];
    }

我将UIButton点击事件处理为

-(void) buttonPressed{

}

我需要有关在事件处理方法中单击了哪个按钮的信息。我需要获取单击按钮的框架。如何更改此代码以获取有关发件人的信息。

【问题讨论】:

    标签: iphone events uibutton sender


    【解决方案1】:

    在创建按钮时添加一个标签,如btn.tag=i; 并重新定义你的方法

    -(void) buttonPressed:(id)sender{
    // compare  sender.tag to find the sender here
    }
    

    【讨论】:

    • 如果我在方法内部没有获得发件人的任何属性,可能是什么问题。
    • 您还需要将选择器更改为buttonPressed:(注意冒号)。如果发件人也总是已知的,您可以使用 UIButton 而不是 id,这样您就不必投射了。
    • 当我将 (id) 类型更改为 (UIButton*) 时,它起作用了。但这不应该也适用于id 类型吗?
    • 是的,我也检查了 .h
    【解决方案2】:

    你应该做的是在你的代码中添加这些行:

    for(int i = 1; i <= count; i++)
    {
    
        if(i ==1){
            UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)];
            [btn1 setImage:image2 forState:UIControlStateNormal];
    
             [btn1 setTag:i];   // This will set tag as the value of your integer i;
    
            [self addSubview:btn1];
            continue;
        }
        x = x + 40;
        y = y + 50; 
         UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(x , y, 40, 40)];
        [btn2 setImage:image1 forState:UIControlStateNormal];
    
        [btn2 setTag:i];   // also set tag here.
        [btn2 addTarget:self action:@selector(buttonPressed) 
       forControlEvents:UIControlEventTouchUpInside];
    
         [self addSubview:btn2];
    }
    

    现在像这样更改您的 IBAction:

     -(void) buttonPressed: (id)sender
     {
    
      if(sender.tag==1) // determine which button was tapped using this tag property.
         {
             // Do your action..
         }
     }
    

    或者你可以这样做..这样你将发送者中的对象复制到这个UIBtton对象。

    -(void) buttonPressed: (id)sender
     {
    
      UIButton *tempBtn=(UIButton *) sender; 
      if(tempBtn.tag==1) // determine which button was tapped using this tag property.
         {
             // Do your action like..
             tempBtn.backgroundColor=[UIColor blueColor];
             tempBtn.alpha=0.5;
         }
     }
    

    所以在这个参数sender 中,它会保存您与之交互的UIButton,您可以访问它的属性,如标签、文本等。希望对您有所帮助..

    【讨论】:

    • 您还需要更改选择器,包括冒号。
    • 我没有在这里获取标签属性。休息一切都很好。发件人未显示任何属性。
    猜你喜欢
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 2012-02-17
    相关资源
    最近更新 更多