【问题标题】:How to pass a variable to a UIButton action如何将变量传递给 UIButton 操作
【发布时间】:2010-10-05 08:30:12
【问题描述】:

例如,我想将变量传递给 UIButton 操作

NSString *string=@"one";
[downbutton addTarget:self action:@selector(action1:string)
     forControlEvents:UIControlEventTouchUpInside];

而我的动作函数是这样的:

-(void) action1:(NSString *)string{
}

但是,它返回一个语法错误。 如何将变量传递给 UIButton 动作?

【问题讨论】:

    标签: ios uibutton


    【解决方案1】:

    改为阅读:

    [downbutton addTarget:self action:@selector(action1:) forControlEvents:UIControlEventTouchUpInside];
    

    我不了解 Iphone SDK,但按钮操作的目标可能会收到一个 id(通常命名为发件人)。

    - (void) buttonPress:(id)sender;
    

    在方法调用中,sender 应该是您案例中的按钮,允许您读取属性,例如它的名称、标签等。

    【讨论】:

      【解决方案2】:

      如果你需要区分多个按钮,那么你可以用这样的标签来标记你的按钮:

      [downbutton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
      downButton.tag = 15;
      

      在您的动作委托方法中,您可以根据之前设置的标签处理每个按钮:

      (void) buttonPress:(id)sender {
          NSInteger tid = ((UIControl *) sender).tag;
          if (tid == 15) {
              // deal with downButton event here ..
          }
          //...
      }
      

      更新:sender.tag 应该是 NSInteger 而不是 NSInteger *

      【讨论】:

        【解决方案3】:

        您可以使用associative references 向您的 UIButton 添加任意数据:

        static char myDataKey;
        ...
        UIButton *myButton = ...
        NSString *myData = @"This could be any object type";
        objc_setAssociatedObject (myButton, &myDataKey, myData, 
          OBJC_ASSOCIATION_RETAIN);
        

        对于策略字段 (OBJC_ASSOCIATION_RETAIN),请为您的案例指定适当的策略。 关于动作委托方法:

        (void) buttonPress:(id)sender {
          NSString *myData =
            (NSString *)objc_getAssociatedObject(sender, &myDataKey);
          ...
        }
        

        【讨论】:

        【解决方案4】:

        传递变量的另一个选项,我发现它比 leviatan 的答案中的标签更直接,是在 accessHint 中传递一个字符串。例如:

        button.accessibilityHint = [user objectId];
        

        然后在按钮的action方法中:

        -(void) someAction:(id) sender {
            UIButton *temp = (UIButton*) sender;
            NSString *variable = temp.accessibilityHint;
            // anything you want to do with this variable
        }
        

        【讨论】:

        • getting (null)...请建议我获取确切的字符串。
        【解决方案5】:

        我发现这样做的唯一方法是在调用操作之前设置一个实例变量

        【讨论】:

          【解决方案6】:

          您可以扩展 UIButton 并添加自定义属性

          //UIButtonDictionary.h
          #import <UIKit/UIKit.h>
          
          @interface UIButtonDictionary : UIButton
          
          @property(nonatomic, strong) NSMutableDictionary* attributes;
          
          @end
          
          //UIButtonDictionary.m
          #import "UIButtonDictionary.h"
          
          @implementation UIButtonDictionary
          @synthesize attributes;
          
          @end
          

          【讨论】:

            【解决方案7】:

            您可以设置按钮的标签并从发送者访问它

            [btnHome addTarget:self action:@selector(btnMenuClicked:)     forControlEvents:UIControlEventTouchUpInside];
                                btnHome.userInteractionEnabled = YES;
                                btnHome.tag = 123;
            

            在被调用函数中

            -(void)btnMenuClicked:(id)sender
            {
            [sender tag];
            
                if ([sender tag] == 123) {
                    // Do Anything
                }
            }
            

            【讨论】:

              【解决方案8】:

              您可以使用您经常使用的 UIControlStates 的字符串:

              NSString *string=@"one";
              [downbutton setTitle:string forState:UIControlStateApplication];
              [downbutton addTarget:self action:@selector(action1:) forControlEvents:UIControlEventTouchUpInside];
              

              和动作函数:

              -(void)action1:(UIButton*)sender{
                  NSLog(@"My string: %@",[sender titleForState:UIControlStateApplication]);
              }
              

              【讨论】:

                猜你喜欢
                • 2021-02-07
                • 2020-08-26
                • 2022-06-16
                • 2017-01-06
                • 1970-01-01
                • 2021-07-11
                • 1970-01-01
                • 2021-02-16
                • 1970-01-01
                相关资源
                最近更新 更多