How to pass a variable to a UIButton action

前端 未结 8 1545
南笙
南笙 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
    }
    

提交回复
热议问题