Dynamically naming a UITextfield

Discussion in 'iOS Programming' started by tazboy, Apr 7, 2010.

  1. tazboy macrumors newbie

    Joined:
    Apr 6, 2010
    #1
    I need to know how to dynamically make UITextfields. I want to make a bunch of textfields and have them each have a different name. This is almost what I need:

    Code:
    UITextfield *textfield[B]+[i][/B] = [[UITextField alloc] initWithFrame:CGRectMake(x,y,30,25)];
    The bold part, +, is what I need to make happen. I've been searching for hours and I haven't found a solution.

    Any help would be greatly appreciated.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Forget about giving them different names. That's only what you might use to access their pointers anyways. You're probably better off creating them in a loop and adding them to a (mutable) array as you go. Something like this:
    Code:
    	NSMutableArray *textfields = [NSMutableArray array];
    	for (int i = 0; i < numTextfields; i++) {
    		UITextField *field = [[UITextField alloc] initWithFrame:...];
    		[textfields addObject:field];
    	}
    P.S. I've left out any memory-management you'll probably want to do as well.
     
  3. tazboy thread starter macrumors newbie

    Joined:
    Apr 6, 2010
    #3
    If I go this route, then would I change the .text value of one of the fields by doing something like this?

    Code:
    textfields[0].text = @"hello";
    Thanks for the assistance.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Yes.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Something like that. (That exactly will give you a "'struct NSMutableArray' has no member named text" error). Remember textfields in a NSMutableArray (which is a subclass of NSArray), so you can't use [n] notation to access its objects. The instance method objectAtIndex: is what you want to use. And there's more you'll need to do to use the setter for the text property.
     
  6. tazboy thread starter macrumors newbie

    Joined:
    Apr 6, 2010
    #6
    Here is what I've tried, along with some other variations, but I can't get it to work.

    Code:
    NSMutableArray *scoreFields, *totalFields, *nameFields;
    @property (nonatomic, retain) NSMutableArray *scoreFields, *totalFields, *nameFields;
    @synthesize scoreFields, totalFields, nameFields;
    I make the scoreFields array. I fill it with UITextFields.
    Code:
    - (void) makeScoreField:(int)i {
    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(10,(i*35)+25,30,25)];
    [self.view addSubview:field];
    [scoreFields addObject:field];
    [field release];
    }
    I'm trying to take 2 user inputted values from UITextFields that are in the scoreFields array. I get the values and make them integers and then add them up. I then make their sum an NSString and put it's value into another UITextField that is stored in the totalFields array.
    Code:
    UITextField *score1, *score2, *scoreTotal;
    NSInteger sumTotal;	
    score1 = [scoreFields objectAtIndex:0];
    score2 = [scoreFields objectAtIndex:1];
    sumTotal = [score1.text integerValue] + [score2.text integerValue];
    scoreTotal = [totalFields objectAtIndex:0];
    scoreTotal.text = [NSString stringWithFormat:@"%d", sumTotal];
    Apparently, my logic does not match the code. I don't think the array's objects are the UITextField objects on the screen. I'm just not getting something.

    Any ideas would be appreciated.
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    So, somewhere, you're creating the NSMutableArrays right? Declaring the properties just allocates a space to store a pointer in, not the array itself...
     
  8. tazboy thread starter macrumors newbie

    Joined:
    Apr 6, 2010
    #8
    Darn rookies can't even remember to alloc and init an array. Thanks man.
     

Share This Page