e.ozmen
e.ozmen

Reputation: 253

Passing 2 parameters into UIButton's action function

I have a UIButton,

-(void)backBtn:(NSString *)parentId lbltext:(NSString *)label

function will be fired when push the button,

[button addTarget:self 
           action:@selector(backBtn:lbltext:) 
       withObject:par 
       withObject:title.text 
 forControlEvents:UIControlEventTouchDown]; 

I looked this question. But my fired function will change my button name, depends some functions with the parameters. So I can't create another class.

How can I make this code true?Thanks.

Upvotes: 1

Views: 461

Answers (1)

Konrad77
Konrad77

Reputation: 2535

I would use a delegate for this instead. Trigger the event in your own class and then send it using a delegate. Also, UIControlEventTouchUpInside is better, UIControlEventTouchDown will be triggered even if the user regrets its action (by moving the finger outside the button).

- (void) touchEvent:(id)sender {
    if ([self.delegate respondToSelector@selector(par:text)]) {
        [self.delegate par:something text:@"MyText"];
    }
}

Upvotes: 3

Related Questions