Reputation: 3000
I have a UIView that is supposed to act like a questionnaire form. In this view I am programatically creating a dynamic amount of UILabels that act as the questions and UITextFields that are where the user puts his answers. This UIView also has a button that you press to move on to the next form.
My question is, lets say I have 10 questions; meaning 10 UILabels with 10 UITextFields in a certain view, is there a way that when the user finishes (clicks the button), I can attain from each UITextField I iterate over, the UILabel value (indicating the question for that answer)?
A simple solution would be to store each UITextField pointer as a key in a dictionary where the UILabel value is its value, but I do not want to use any more memory or data structures.
I also know that when using the StoryBoard you can connect the labels to the textfields via an outlet.
I tried looking to see if the UITextField has an hidden variable in which I can store the UILabel value, but did not find anything like that.
Is there a programatical, easy, no additional memory solution for this?
Upvotes: 0
Views: 173
Reputation: 753
Perhaps use tags. Iterate through all your UILabels and UITextfields...and when the tags are the same, grab the UILabel text property value and do your appropriate work with it.
Upvotes: 0
Reputation: 119031
You could set the tag
of each so that the label has a unique tag and the associated text field is +1.
Or you could base it on the frame origin position while you're looping.
Or the 'hidden' variable route would be by using objc_setAssociatedObject
Upvotes: 1
Reputation: 11026
yes, you can give tag to each UITextField and UILabel and when done you know the tag, so from the tag you can checkout the view and do whatever you want to do.
Upvotes: 1