-2
NSString *q = @"Day";
NSArray *shu = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%@1",q];
[[self.shu objectAtIndex:0] setText: @"HI"];

In the following code I have an array that I am filling up with NSStrings. What I am trying to do is fill up the array with a UITextfield objects not a string of the name. Is it possible to convert a string to a object UITextfield? Something like this:

NSString *q = @"Day";
NSArray *shu = [NSArray arrayWithObjects:[NSObject namewithFormat:@"%@1",q];
[[self.shu objectAtIndex:0] setText: @"HI"];

EDIT: What I want to do is using this code:

for (int i=1; i<=5; ++i) { 
NSArray *shu = [NSArray arrayWithObjects:[NSString stringWithFormat:@"TextField%d1",i],[NSString stringWithFormat:@"TextField%d2",i];
 }

Make it so that when i = 1 then:

NSArray *shu = @[Textfield11,Textfield12];

And when i = 2 then:

NSArray *shu = @[Textfield21,Textfield22];
  • Your question does not make sense. I suggest you look up the topics *variable*, *lifetime*, *scope* and *objects* in Objective-C; you may be used to a language where global variables are created on the fly, this is not the case for most compiled languages like (objective-)C(++). You may find [this answer](http://stackoverflow.com/questions/21508537/nsdictionary-objectforkey-to-var-name/21508748#21508748) helpful. – CRD Feb 03 '14 at 00:42

1 Answers1

0

If I understand what you're asking for, you want to set a property of a view controller based on it's name.

You should look into key value coding. There are methods setValue:forKey and setValue:forKeyPath.

setValue:forKey attempts to find a property or instance variable of the target object and change the value of that proerty.

setValue:forKeyPath will traverse a list of objects. So, for example, if you have a view controller called fooVC, and it has a text field called fooTextField.

you could use code like this:

[fooVC setValue: @"HI" forKeyPath: "fooTextField.text"];

That would search for a property called "fooTextField" in the object "fooVC", and then search for a property called "text" in the "fooTextField" object. If both exist, it would set the value of the text property to the string "HI".

Duncan C
  • 128,072
  • 22
  • 173
  • 272