Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tazboy

macrumors newbie
Original poster
Apr 6, 2010
8
0
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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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.
 

tazboy

macrumors newbie
Original poster
Apr 6, 2010
8
0
Code:
	NSMutableArray *textfields = [NSMutableArray array];
	for (int i = 0; i < numTextfields; i++) {
		UITextField *field = [[UITextField alloc] initWithFrame:...];
		[textfields addObject:field];
	}

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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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";
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.
 

tazboy

macrumors newbie
Original poster
Apr 6, 2010
8
0
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
So, somewhere, you're creating the NSMutableArrays right? Declaring the properties just allocates a space to store a pointer in, not the array itself...
 

tazboy

macrumors newbie
Original poster
Apr 6, 2010
8
0
So, somewhere, you're creating the NSMutableArrays right? Declaring the properties just allocates a space to store a pointer in, not the array itself...

Darn rookies can't even remember to alloc and init an array. Thanks man.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.