6

I've created a custom UIView class FormDropdown, which contains a question & button in the nib. In the class is also an NSArray property which is supposed to store the various options for the button.

So a button can be placed by doing this, in for instance a viewDidLoad method:

FormDropdown *dropdown = [FormDropdown dropdownWithQuestion:@"This is an example question" andLabel:@"Select one" andOptions:[NSArray arrayWithObjects:@"One", @"Two", @"Three", nil]];
[self.view addSubview:dropdown];

Obviously, I'd like the button to, when tapped, bring up a UIPickerView with the options showing. But I'm stuck on how to send the options to any method. I know I can attach an action to the button like so:

[dropdown.dropdownButton addTarget:self action:@selector(dropdownPressed:) forControlEvents:UIControlEventTouchUpInside];

..but I can't see how I would pass the options from the dropdown.options array to the method?

cannyboy
  • 24,180
  • 40
  • 146
  • 252
  • You can use the solution provided in this answer regarding UIButton and passing mulitple parameters to its selector: https://stackoverflow.com/a/53779104/5324541 – Lloyd Keijzer Dec 14 '18 at 11:58

2 Answers2

1

I believe that you can do this by adding an "associative reference" from the UIButton to your object data.

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocAssociativeReferences.html

user102008
  • 30,736
  • 10
  • 83
  • 104
0

I am looking for a way to do that as well... however, it doesn't seem possible.

My possible solution: I think I am going to create a subclass of UIButton, and add a "NSObject *tagObject" property to it.

Anyone seems something wrong about it? (I am using ARC, and I am wondering if that would cause objects to remain in memory - I do not think so).

rufo
  • 5,158
  • 2
  • 36
  • 47