Dynamically Assigning UITextfield outlets

Discussion in 'iOS Programming' started by newConvert, Apr 7, 2009.

  1. newConvert macrumors regular

    Aug 25, 2006
    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!
  2. CocoaPuffs macrumors 68010

    Aug 23, 2008
  3. johnnyjibbs macrumors 68030


    Sep 18, 2003
    London, UK
    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.
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    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.
  5. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    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.
  6. newConvert thread starter macrumors regular

    Aug 25, 2006
    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?


    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.
  7. eddietr macrumors 6502a

    Oct 29, 2006
    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.

Share This Page