2010-12-02 14 views
0

Je programme une application iphone et j'ai besoin d'aide avec les UILabels. Un exemple: J'ai 10 étiquettes nommées Label1, Label2, Label3, etc. boucle. Comme pour (int i = 1, i < 11, i ++) {Labeli.text = ...}? merci d'aider.UILabels indexés Objective-C

Répondre

0

est ici la bonne boucle:

for(int i = 0; i < numLabels; i++) { 
    UILabel *label = [[UILabel alloc] init]; 
    label.text = [NSString stringWithFormat:@"%i", i]; 
    // other label customizations here 
    [myMutableArray addObject:label] 
    [label release]; 
} 

Puis, plus tard, accéder à chaque étiquette avec [myMutableArray objectAtIndex:index];

1

Set balise pour chaque étiquette et accéder à la valeur comme suit,

for(int i=1; i<=10;i++) 
{ 
    UILabel *lab=(UILabel *)[self.view viewWithTag:i]; 
    [lab setText:[NSString stringWithFormat:@"%d",i]]; 
} 

ou sinon, procédez comme suit,

NSArray *labels=[NSArray arrayWithObjects: label1, label2, nil]; 

int i=1; 

for(UILabel *label in labels) { 
    [label setText:[NSString stringWithFormat:@"%d",i]]; 
    i++; 
} 
+0

Dans le premier bloc, quand il met l'étiquette, pourquoi ne pas simplement définir le texte? –

+0

Il peut définir diff texte plus tard na ?? – KingofBliss