Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing object data with UIButton press

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?

like image 337
cannyboy Avatar asked Nov 08 '10 11:11

cannyboy


People also ask

How to pass an object as an argument to a function?

Inside the function, the integer value of the argument object is added to that on which the ‘add’ function is called. In this method, we can pass objects as an argument and alter them.

What is message passing in Java?

The idea of message passing is “ sending object will just send a message to one object and the other object will decide what to do ”.

How do object objects communicate with each other?

Objects are a representation of real-world and objects communicate with each other via messages. When two or more objects communicate with each other that means that those objects are sending and receiving messages. This is often called method calling.

How do you pass data from one component to another component?

We can pass data to different components using parameters. If we add [Parameter] attribute to any public properties of a component, it becomes a parameter. If we add [Parameter] attribute to any public properties of a component, the property will become a parameter that can be passed to the component.


1 Answers

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

like image 172
user102008 Avatar answered Sep 29 '22 02:09

user102008