2012-05-14 68 views
0

我想用4個按鈕創建一個行,循環正在做它應該做的事情,並且它只進入if語句4次,但是當視圖彈出時,我可以看到只有一個按鈕。按鈕不會添加到視圖

這是爲什麼?難道我做錯了什麼?

btnFrame = 18; 
for (int i = 0; i < [arrImages count]; i++) 
{ 
    if (btnFrame <= 237) 
    { 
     NSLog(@"%i",btnFrame); 
     UIButton * tempBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
     tempBtn.frame = CGRectMake(btnFrame, 20, 65, 65); 
     [tempBtn setTitle:[NSString stringWithFormat:@"Button%i",i] forState:UIControlStateNormal]; 
     [self.view addSubview:tempBtn]; 
     btnFrame = btnFrame + 73; 
    } 
} 

非常感謝!

+3

你在哪裏把這個代碼? – rdelmar

+0

ViewDidLoad,實際上它與這個線程無關。不管怎麼說,還是要謝謝你。 –

+0

此代碼按預期工作。正如rdelmar問,你把這個代碼放在哪裏? –

回答

1

我認爲當你的viewWill在那個時候會消失,那麼這個視圖將被清除,所以做下面的事情.... 如果你克里特一種方法,你只需粘貼你的上面的代碼,然後當你的ViewController出現在那個時候調用你的方法試試這...

-(void)viewWillAppear:(BOOL)animated 
{ 
    [self SetButton];  
} 

-(void)setButton{ 

btnFrame = 18; 
    for (int i = 0; i < [arrImages count]; i++) 
    { 
     if (btnFrame <= 237) 
     { 
      NSLog(@"%i",btnFrame); 
      UIButton * tempBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
      tempBtn.frame = CGRectMake(btnFrame, 20, 65, 65); 
      [tempBtn setTitle:[NSString stringWithFormat:@"Button%i",i] forState:UIControlStateNormal]; 
      [self.view addSubview:tempBtn]; 
      btnFrame = btnFrame + 73; 
     } 
    } 
} 

希望這有助於你.... :)

+0

它可以工作,但無論如何。 :) –