【问题标题】:Passing data to a method when a button is tapped点击按钮时将数据传递给方法
【发布时间】:2012-12-31 11:19:45
【问题描述】:

我在我的 iphone 应用程序中为地图视图页面的注释标记添加了一个按钮

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
                action:@selector(go_to_detail_page:)    
      forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = rightButton; 

而我的接收函数是

-(IBAction)go_to_detail_page:(id)sender;{
}

我的问题如下。我在我的页面上创建了很多标记,我想在按下特定的注释视图按钮时传递一个唯一标识符,即使是一个字符串也可以。按下注释后,如何将字符串传递给go_to_detail_page 方法?

【问题讨论】:

  • 为什么不用“sender”来识别呢?按钮本身作为其操作选择器的参数传递。
  • 抱歉,我该怎么做?谢谢
  • @user1096447 : 请使用良好的命名约定...方法名使用驼峰命名法。
  • 您不需要使用 IBAction。取而代之的是 void。 IBAction 或 IBOutlet 旨在与 IB(接口生成器)一起使用。它们只是占位符。在引擎盖下,它们意味着无效。

标签: objective-c ios annotations uibutton mapkit


【解决方案1】:

使用rightButton.tag = 1 并在

-(IBAction)go_to_detail_page:(id)sender{
    UIButton *button = (UIButton *)sender;
    if(button.tag==1){//this is the rightButton
         //your logic goes here

    }
}

【讨论】:

    【解决方案2】:

    嘿,你可以对 UIButton 进行子类化并将 NSString* 成员标记为每个按钮实例..

    【讨论】:

    • 哈哈...这就像去金星并从那里借用一氧化碳...
    • UIButton 包含一个叫做标签的东西,你可以使用它,不需要子类和添加另一个 ivar。
    • 对标准控件进行子分类以包含上下文数据是一种公认​​的做法。如果您有大量动态分配的对象,那就是我试图传达 Anoop Vaidya saab 的方法!大声笑
    • 在这种情况下我更喜欢使用关联引用。
    • @AnoopVaidya 用于存储信息的子类化控件很好。一个简单的整数标签无法容纳整个数据库。
    【解决方案3】:

    在我看来,您可以有两种选择。

    第一个选项是为每个按钮分配一个tag,然后在其操作中检索它。因此,例如,您将为每个按钮分配一个不同的标签。

    rightButton.tag = // a tag of integer type
    

    然后你会像这样使用

    - (void)goToDetailedPage:(id)sender
    {
        UIButton *senderButton = (UIButton *)sender;
    
        int row = senderButton.tag;        
        // do what you want with the tag
    }
    

    另一种选择是使用关联引用。通过它们,无需子类化UIButton,您只需创建一个属性(NSString 类型)并将其用作标识符。

    要使用它们,请查看Subclass UIButton to add a property

    这是一个相当复杂的概念,但通过它你有很大的灵活性。

    注意事项

    您不需要使用 IBAction。取而代之的是 void。 IBAction 或 IBOutlet 旨在与 IB(接口生成器)一起使用。它们只是占位符。在引擎盖下,它们意味着无效。

    使用驼峰式表示法。例如,正如我在回答中所写,使用 goToDetailedPage 而不是 go_to_detail_page

    【讨论】:

      【解决方案4】:

      您可以将唯一标识符设置为按钮tag

      UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
      [rightButton addTarget:self
                      action:@selector(go_to_detail_page:)    
            forControlEvents:UIControlEventTouchUpInside];
      annView.rightCalloutAccessoryView = rightButton; 
      rightButton.tag == any unique number // it would act as unique identifier
      

      并按如下方式检索它

      - (IBAction)go_to_detail_page:(id)sender;{
      
          UIButton *button = (UIButton *)sender;
          if(button.tag==unique identifier){
              // this is the rightButton
              // your logic
          }
          else
          {
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-09
        • 2011-12-21
        相关资源
        最近更新 更多