2010-02-24 7 views
1

Est-il possible de définir le champ de texte pour un UITextField pour plusieurs objets à l'intérieur d'une boucle en utilisant setValue: forKey :? Je suis un peu confus si je devrais être en quelque sorte en train de spécifier "texte" dans le nom de la propriété, ou s'il me manque quelque chose d'autre?Définition du texte UITextField dans une boucle?

// INTERFACE 
@property(nonatomic, retain) IBOutlet UITextField *textFieldBit_01; 
@property(nonatomic, retain) IBOutlet UITextField *textFieldBit_02; 
@property(nonatomic, retain) IBOutlet UITextField *textFieldBit_03; 
@property(nonatomic, retain) IBOutlet UITextField *textFieldBit_04; 
@property(nonatomic, retain) IBOutlet UITextField *textFieldBit_05; 

.

// IMPLEMENT 
@synthesize textFieldBit_01; 
@synthesize textFieldBit_02; 
@synthesize textFieldBit_03; 
@synthesize textFieldBit_04; 
@synthesize textFieldBit_05; 

for(unsigned int counter=1; counter<=5; counter++) { 
    NSString *propertyName = [[NSString alloc] initWithFormat:@"textFieldBit_%02d.text",counter]; 
    NSString *propertyValue = [[NSString alloc] initWithFormat:@"%d", counter]; 

    [self setValue:propertyValue forKey:propertyName]; 
    [propertyName release]; 
    [propertyValue release]; 
} 

EDIT_001

Peut-être que je devrais essayer de clarifier ma question un peu: Je suis essentiellement en train de mettre le texte sur l'un des cinq UITextFields chaque fois autour d'une boucle. Je pourrais utiliser un "si" ou un "switch" mais cela ressemble un peu à un surclassement. Je pensais que setValue: forKey: me laisserait le faire en construisant une clé unique à chaque itération et en utilisant un seul appel.

if(counter == 1) [textFieldBit_01 setText:@"%02d", 1]; 
if(counter == 2) [textFieldBit_02 setText:@"%02d", 2]; 
if(counter == 3) [textFieldBit_03 setText:@"%02d", 3]; 
if(counter == 4) [textFieldBit_04 setText:@"%02d", 4]; 
if(counter == 5) [textFieldBit_05 setText:@"%02d", 5]; 

EDIT_002

NSString *propertyName = [[NSString alloc] initWithFormat:@"labelBit_%02d.text",counter]; 
NSString *propertyValue = [[NSString alloc] initWithFormat:@"%d", geoTag];  
[self setValue:propertyValue forKeyPath:propertyName]; 

Merci pour la réponse, je comprends maintenant où je me trompe, ce que j'utilisais était setValue: forKey: quand dans une situation où je avais besoin d'accéder à « labelBit_01 .text » Je l'ai utilisé setValue: forKeyPath:

gary

Répondre

2

Essayez

[self setValue:propertyValue forKeyPath:[propertyName stringByAppendingString:@".text"]]; 

Ce que cela finit par vous donner est la suivante:

self.textFieldBit_01.text = @"01"; 
+0

Un grand merci, setValue: forKeyPath: plutôt que setValue: forKey: :) – fuzzygoat

Questions connexes