How to pass a variable to a UIButton action

前端 未结 8 1541
南笙
南笙 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:12

    You can use the strings of the UIControlStates that you dot'n use:

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

    and the action function:

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

提交回复
热议问题