2011-01-22 9 views
1

私のハングアップは、配列を作成してボタンを渡し、forループを使用して各ボタンをビューに配置します。彼らは両方が同じCGRectMakeを通過するので、彼らは同じポジションにあることを知っています。しかし、私はこのコードで画面上に何かを置くことはできませんが、エラーを返さないにもかかわらず、なぜ私が困惑してしまっています。基本的なObjective-C配列 'for'ループアシスタント

誰もが何か提案がありますか?ありがとう!

- (void)practiceMethod{ 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Practice Method has begun" message:@"Alert" delegate: self cancelButtonTitle:@"Close" otherButtonTitles: nil]; 
//[alert show]; 
[alert release]; 

float i=0; 

CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; 

UIImage *buttonGraphic = [UIImage imageNamed:@"1.png"]; 

UIButton *buttonOne = [[UIButton alloc] initWithFrame:applicationFrame]; 
UIButton *buttonTwo = [[UIButton alloc] initWithFrame:applicationFrame]; 

buttonArray = [[NSArray alloc] initWithObjects:buttonOne, buttonTwo,nil]; 

for (i = 0; i > [buttonArray count]; i++) { 

    UIButton *tempElement = [buttonArray objectAtIndex:i]; 

    tempElement.frame = CGRectMake(140, 230, 50, 50); 

    [tempElement setBackgroundImage:buttonGraphic forState:UIControlStateNormal]; 

    [self addSubview:tempElement]; 
} 
} 

答えて

8

変更

for (i = 0; i > [buttonArray count]; i++) {

for (i = 0; i < [buttonArray count]; i++) {

に、あなたが起動して実行することでしょう。

PS。置き換え

for(UIButton* button in buttonArray) 
{ 
    // stuff with button 
} 
+0

1人:私はばかです。 2:ありがとうございます:) –

+1

これらのことが起こります。私の最も一般的な 'for 'の間違いは' for unsigned int i = arrayLength-1; i> 0; -i); ' :D –

3

for (i = 0; i > [buttonArray count]; i++) { 
    UIButton *tempElement = [buttonArray objectAtIndex:i]; 
    ... 

} 

を配列ループショートカット構文で:

for (UIButton *tempElement in buttonArray) { 
    ... 

} 

をそれを使用することも良いでしょう私は、あなたが問題のこれらの種類のためのObjective-CS for eachイテレータを使用することをお勧めUButton付きbuttonWithType:

UIButton *buttonOne = [UIButton buttonWithType: UIButtonTypeCustom]; 
buttonOne.frame = applicationFrame; 
+0

私はgraham - use' UIButton buttonWithType: 'に同意します –

+0

Fast Enumeration isあなたの可読性の友人! –

+0

ヒントをありがとう:) –

関連する問題