问题
I'm using a UITextfield to input information. I have several UITextfields that need to equal the input UITextfield. I'm using this bit of code:
tex34.text = tex33.text;
How do I expand the code to handle multiple textfields?
回答1:
There's no 'broadcast' idea that will allow you to set them all with one statement. You'll just have to have as many assignments as you have text fields. If you have a billion of them, you could write some Perl to output the code.
e.g.
tex34.text = text33.text;
tex35.text = text33.text;
tex36.text = text33.text;
tex37.text = text33.text;
If you're in a tight loop, you can save some property read invocations using a temporary string:
const NSString *text = tex33.text;
tex34.text = text;
tex35.text = text;
//etc
If you really have a lot of them (unlikely), you can build the text field variable names as NSStrings and use key-value-coding to update their text property:
const NSString *text = tex33.text;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (int i = 34; i < 1653; i++) {
const NSString *key = [NSString stringWithFormat:@"tex%d.text", i];
[self setValue:text forKey:key];
if (i % 100 == 0) [pool drain];
}
[pool release];
This approach will require that you have each texXX
set up as a property...
来源:https://stackoverflow.com/questions/9507875/how-to-use-uitextfields-text-in-multiple-uitextfields