How to pass a variable to a UIButton action

前端 未结 8 1498
南笙
南笙 2020-12-13 21:05

I want to pass a variable to a UIButton action, for example

NSString *string=@\"one\";
[downbutton addTarget:self action:@selector(action1:string)
     forCo         


        
相关标签:
8条回答
  • 2020-12-13 21:27

    Another option for passing variables, which I find to be more direct than the tag from leviatan's answer is to pass a string in the accessibilityHint. For example:

    button.accessibilityHint = [user objectId];
    

    Then in the action method of the button:

    -(void) someAction:(id) sender {
        UIButton *temp = (UIButton*) sender;
        NSString *variable = temp.accessibilityHint;
        // anything you want to do with this variable
    }
    
    0 讨论(0)
  • 2020-12-13 21:34

    You can extends UIButton and add a custom property

    //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
    
    0 讨论(0)
提交回复
热议问题