ios - for-loop problems changing property of all objects in array -


my first time , hope is/was not problem me.

upfront, create , stuff without .nib, .xib or storyboard , quite new xcode.

problem description:

i create series of labels, go array programmatically.

int below or equal 4 else not fire anyway.

int = (int)[players count];  if (i<=4) {      namelabel = [[uilabel alloc]initwithframe:cgrectmake(self.view.bounds.size.width-(self.view.bounds.size.width/3)-20*widthmultiplayer, self.view.bounds.size.height-((50*heightmultibplyer)*i)-50*heightmultibplyer, (self.view.bounds.size.width/3), 50*heightmultibplyer)];     namelabel.text = entername.text;      [namelabel setfont:[uifont fontwithname:@"arial-boldmt" size:fontsize]];     [namelabel settextalignment:nstextalignmentright];     [self.view addsubview:namelabel];     players[i] = namelabel;     nslog(@"index: %i , name:%@", i, namelabel.text);      entername.text = @""; } 

then hide labels programmatically following code

for (uilabel *label in players) {     label.hidden = yes; } 

now funny part - when want show them again using

for (uilabel *label in players) {     label.hidden = no; } 

it not show last entry.

meaning:

  • if have 1 entry shows nothing
  • if have 3 entries shows 2
  • and on , on...

funny thing log of array shows objects, shows correct index of each object , hidden status false (-so should be).

tried bring views front

[self.view bringsubviewtofront:label] 

but wouldn't work well.

i can't figure out why. answers , tips on wrong highly appreciated.

---- edit ---- found workaround problem, maybe helps someone: changed change of hidden value little doing this:

        (uilabel *label in players) {         [label removefromsuperview];         label.hidden = no;         [self.view addsubview:label];     } 

looks cheesy, cheesy, works. still if can me find solution or major bug, love it.

ok worked whole theory part in question , result got worked without issues

here code used testing

players=[[nsmutablearray alloc]initwithcapacity:3];   (int k=0; k<10; k++) {     nslog(@"%@",nsstringfromcgrect(cgrectmake(self.view.bounds.size.width-(self.view.bounds.size.width/3)-20*widthmultiplayer, self.view.bounds.size.height-((50*heightmultibplyer)*k)-50*heightmultibplyer, (self.view.bounds.size.width/3), 50*heightmultibplyer)));  } int i=0; cgrect rect=cgrectmake(self.view.bounds.size.width-(self.view.bounds.size.width/3)-20*widthmultiplayer, self.view.bounds.size.height-((50*heightmultibplyer)*i)-50*heightmultibplyer, (self.view.bounds.size.width/3), 50*heightmultibplyer); nslog(@"%@",nsstringfromcgrect(rect)); namelabel = [[uilabel alloc]initwithframe:rect]; [namelabel setbackgroundcolor:[uicolor bluecolor]]; [namelabel.layer setbordercolor:[uicolor blackcolor].cgcolor]; [self.view addsubview:namelabel]; players[0] = namelabel;   i=1; namelabel = [[uilabel alloc]initwithframe:cgrectmake(self.view.bounds.size.width-(self.view.bounds.size.width/3)-20*widthmultiplayer, self.view.bounds.size.height-((50*heightmultibplyer)*i)-50*heightmultibplyer, (self.view.bounds.size.width/3), 50*heightmultibplyer)]; [namelabel setbackgroundcolor:[uicolor redcolor]]; [namelabel.layer setbordercolor:[uicolor blackcolor].cgcolor];  [self.view addsubview:namelabel];  players[1] = namelabel; nslog(@"%@",players); = (int)[players count]; if (i<=4) {     nslog(@"%@",players);     namelabel = [[uilabel alloc]initwithframe:cgrectmake(self.view.bounds.size.width-(self.view.bounds.size.width/3)-20*widthmultiplayer, self.view.bounds.size.height-((50*heightmultibplyer)*i)-50*heightmultibplyer, (self.view.bounds.size.width/3), 50*heightmultibplyer)];     [namelabel setbackgroundcolor:[uicolor greencolor]];     [namelabel.layer setbordercolor:[uicolor blackcolor].cgcolor];     [self.view addsubview:namelabel];     players[i] = namelabel;     nslog(@"index: %i , name:%@", i, namelabel.text);      nslog(@"%@",players); }     (uilabel *label in players) {     label.hidden = yes; } nslog(@"%@",players); (uilabel *label in players) {     label.hidden = no; }  nslog(@"%@",players); 

now explanation of may have went wrong.this case can occour due reason one of label added array not added view.that reason when re-add turns working


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -