Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a parameter to this function?

I have the following code:

[replyAllBtn addTarget:self.target action:@selector(ReplyAll:) forControlEvents:UIControlEventTouchUpInside];

- (void)replyAll:(NSInteger)tid {
// some code
}

How can I send a parameter to the ReplyAll function?

like image 723
Vijayeta Avatar asked Jan 16 '09 12:01

Vijayeta


3 Answers

The replyAll method should accept (id)sender. If a UIButton fired the event, then that same UIButton will be passed as the sender. UIButton has a property "tag" that you can attach your own custom data to (much like .net winforms).

So you'd hook up your event with:

[replyAllBtn addTarget:self.target action:@selector(ReplyAll:) forControlEvents:UIControlEventTouchUpInside];
replyAllBtn.tag=15;

then handle it with:

(void) ReplyAll:(id)sender{
    NSInteger *tid = ((UIControl*)sender).tag;
    //...
like image 144
Rob Fonseca-Ensor Avatar answered Nov 09 '22 16:11

Rob Fonseca-Ensor


A selector function will normally be defined as such:

- (void) ReplyAll:(id)sender;

So the only parameter an action will ever receives is the actual control that called it. You could just add a property to your control that can be read in replyAll

like image 39
MLefrancois Avatar answered Nov 09 '22 17:11

MLefrancois


If you want to send an int value, set the tag of the button = the int value you want to pass. Then you can access the tag value of the button to get the int you wanted.

NSInteger is not a pointer. Try this

NSInteger tid = sender.tag;
like image 21
lostInTransit Avatar answered Nov 09 '22 18:11

lostInTransit