【问题标题】:@selector with multiple arugments [duplicate]带有多个参数的@selector [重复]
【发布时间】:2013-03-02 05:46:03
【问题描述】:

好的,所以Objective-C: Calling selectors with multiple arguments 可以工作,但是我如何使用addTarget:action:forControlEvents: 来实现UIButton?没有一个有withObject: 我不认为......我该怎么办?

【问题讨论】:

    标签: objective-c methods uibutton arguments selector


    【解决方案1】:

    如果你想为UIButton 传递多个参数,你应该传递一个Dictionary 参数

    有两种处理方式:

    1. 您可以使用实例变量来保存您要使用的值。

    例如:

    - (void)buttonPressed:(id)sender
    {
        NSMutableDictionary *argDictionary = _ivar;
    }
    

    2.您可以使用类别来伪造属性。

    如何伪造类别中的属性,您可以查看:http://oleb.net/blog/2011/05/faking-ivars-in-objc-categories-with-associative-references/

    然后你可以使用:

    - (void)buttonPressed:(UIButton *)sender
    {
        // use sender.argDictionary to get your fake property
    }
    

    更多信息你可以看到my answer before

    【讨论】:

      【解决方案2】:

      一种可能性是创建一个自定义 UIButton 扩展接口,其中包含您需要传递的所有属性,在创建按钮时设置按钮的该属性,然后在调用操作后访问它。例如:

      @interface UIButton (additions)
      
      @property NSString * customPropertyString;
      
      @end
      

      然后,按通常的方式添加目标和选择器:

      [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
      

      最后,handler方法的实现:

      - (void)buttonPressed:(id)sender
      {
          UIButton * senderButton = (UIButton *)sender;
          NSLog(@"%@", senderButton.customPropertyString);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-25
        • 2016-07-13
        • 2020-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多