perfomSelector on button and sending parameters

人盡茶涼 提交于 2019-12-02 13:13:11
D25

You can make your UIButton call a function in between (like a middle man), to control the next functions parameters.

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

-(void) buttonTapped:(id)sender{
    if(<some logical condition>){
        [self showImg:sender string1:@"-1" string2:@"-1"];
    }else {
        [self showImg:sender string1:@"otherVal1" string2:@"otherVal2"];
    }
}

-(void) showImg:(id)sender string1:(NSString *)string1 string2:(NSString*)string2 {
    //Other logic
}

The following line is wrong :

[myButton performSelector @selector(showImg:string1:string2::) withObject:@"-1" withObject:@"-1"];

You can't pass parameters like this to the selector, that's why you have an error. I don't think you can pass multiple parameters to a selector. Maybe you can try to set a tag to your button with your const value (works with integers)

For example :

//Init your button......
[myButton setTag:1];
[myButton addTarget:self action:@selector(showImg:) forControlEvents: UIControlEventTouchUpInside];

- (void)showImg:(id)sender {
  UIButton *btn = (UIButton *)sender;
  int value = btn.tag;
}

Just a suggestion.. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!