PDA

View Full Version : Dynamically Assigning UITextfield outlets




newConvert
Apr 7, 2009, 05:16 PM
Just a quick (hopefully!) question for some of you more experiance people out there....

In my view I have about 6 textboxes, however, due to the way my app works I do not want to have to create an individual outlet for each one... I was wondering how you go about re-assigning a different textbox to the one outlet you have? Thanks!



CocoaPuffs
Apr 7, 2009, 06:25 PM
Manually code the textbox.

johnnyjibbs
Apr 8, 2009, 04:18 AM
I second that. The quicker you can ween yourself off of Interface Builder the better. It's great when you're starting out, but it soon becomes easy to imagine the layout programmatically. Coding everything is much more flexible.

dejo
Apr 8, 2009, 09:08 AM
Interface Builder usage is mostly a matter of opinion. Mostly. Some people, like johnnyjibbs, try to avoid it. Others consider it just another tool in their arsenal. Sure, there are some things you can't do with IB but it does have other advantages. If you feel comfortable using it and are working alone, no need to ween yourself off it. If you are working as a team, the team needs to decide whether to keep it or lose it. Hope that helps.

PhoneyDeveloper
Apr 8, 2009, 10:44 AM
Your question is a little vague.

You can use tags to find a subview. Just set the tags in IB for your text fields and then use viewWithTag to find the text field that you want.

If that's not the answer you need to describe the problem better.

newConvert
Apr 8, 2009, 04:41 PM
Sorry for being unclear, this is what I'm looking for...

Currently, I have 6 textboxes (might be as high as 15-20 in the end) and I'm looking for a way to have just one IBOutlet, and assign it whichever textbox I need to get input to Does that make sense?

IE

IBOutlet UITextfield *onlyOutlet;

Then if a certain variable is 5, I want to assign this outlet to the 5th textbox, just not sure of the syntax? Thanks.

eddietr
Apr 8, 2009, 06:04 PM
So the syntax is simple. It's just:


onlyOutlet = idOfSomeTextField


the more interesting part will be keeping track of the id's of all the TextFields. You might just keep an array for those. And then create the textfields programatically and keep the id's in your array.

Or if you are using IB, then tag the textfields and look them up that way and put them into an array after the Nib is loaded.