Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send an object as argument when a UIButton is tapped?

I have the following code inside my UIViewController -

[flipButton addTarget:self.navigationController.delegate action:@selector(changeModeAction:) forControlEvents:UIControlEventTouchUpInside];

As you can see, it calls a method inside it's navigation controller delegate. How do I correctly pass along an object to this method?

like image 994
skålfyfan Avatar asked Dec 12 '22 14:12

skålfyfan


1 Answers

You can use the layer property. Add the object you want to pass as the value in the layer dictionary.

[[btn layer] setValue:yourObj forKey:@"yourKey"];

This yourObj is accessible from the button action function:

-(void)btnClicked:(id)sender
{
    yourObj = [[sender layer] valueForKey:@"yourKey"];
}

With this method you can pass multiple values to the button function just by adding new objects in the dictionary with different keys.

like image 105
Tinku George Avatar answered Mar 02 '23 15:03

Tinku George