2011-10-25 6 views
0

私はこの次のコードで立ち往生しています:目的C:配列からuibuttonを作成する際にエラーが発生しましたか?

switch (i) 
    case 0: { 
     /* 
     ..... 
     repeat this code 
     ..... 
    } 

は、このように使用するスイッチケースならば、コードは非常に長い:

btn1 = [UIButton alloc]; 
btn2 = [UIButton alloc]; 
btn3 = [UIButton alloc]; 
btn4 = [UIButton alloc]; 
btn5 = [UIButton alloc]; 
btn6 = [UIButton alloc]; 
NSArray *myarray = [NSArray arrayWithObjects:btn1, btn2, btn3, btn4, btn5, btn6,nil]; 

for (int i =0; i < [myarray count]; i++) 
{ 
    NSLog(@"What: %@", [myarray objectAtIndex:i]); // Result UIButton 
    [myarray objectAtIndex:i].frame = CGRectMake(i* 110+25, 60, 100, 100);// error, cant build 
    UIButton *b = (UIButton *)[myarray objectAtIndex:i]; 
    b.frame = CGRectMake(i* 110+25, 60, 100, 100); 
    [b setTitle:@"my button" forState:UIControlStateNormal]; 
    b.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
    b.titleLabel.textAlignment = UITextAlignmentCenter; 
    b.titleLabel.textColor = [UIColor colorWithRed:0x93/255.0 green:0x73/255.0 blue:0x47/255.0 alpha:1.0]; 
    b.tag = i+1; 
    [b addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown]; 
    [self addSubview: b]; 
    [b release]; 
} 

は、もともと私は次のように、スイッチケースを使用します。

  • 位置
  • タイトルを設定するようになどのターゲットを追加

それとも、単純に/短いコードを持っているか、背景、コードを繰り返さないようにしてください:私はループそれがある理由?

あなたがのallocした後に初期化するために必要
+0

各実行時にボタンが異なる可能性があります.Xcode/Interface Builderを使用してGUIを構築することができない場合は、いくつかのコードを調整しますか? – Mark

答えて

3

あなたはUIButtonを割り当てていますが、初期化していません。

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 10, 10)]; 

はまた、 '[myarrayのobjectAtIndex:I]' のすべてのインスタンスを置き換えるループその後、

UIButton *currentButton = [myarray objectAtIndex:i]; 

にUIButtonポインタをキャッシュする必要があると 'currentButton'

0
btn1 = [UIButton alloc]; 

そうでない場合、あなたはそれならば、それはそのためのオブジェクトが何であるか、クラスを知っているといないため、ガベージメモリ

btn1 = [[UIButton alloc] init]; 

また

[myarray objectAtIndex:i].frame = CGRectMake(i* 110+25, 60, 100, 100);// error, cant build 

が失敗し、割り当てられていますそれをsetFrame:を呼び出すことができます。なぜなら、それをボタン(UIViewサブクラス)としてキャストする必要があるからです。

UIButton *b = (UIButton *)[myarray objectAtIndex:i]; 
b.frame = CGRectMake(i* 110+25, 60, 100, 100); 
0

あなたはこれを試すこと?

for (int i =0; i < [myarray count]; i++) 
{ 
    UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(i* 110+25, 60, 100, 100)]; 
    [b setTitle:@"my button" forState:UIControlStateNormal]; 
    b.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
    b.titleLabel.textAlignment = UITextAlignmentCenter; 
    b.titleLabel.textColor = [UIColor colorWithRed:0x93/255.0 green:0x73/255.0 blue:0x47/255.0 alpha:1.0]; 
    b.tag = i+1; 
    [b addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown]; 
    [self addSubview: b]; 
    [b release]; 
} 

あなたのボタンを初期化するのを忘れたと思います。それらを初期化する方法はいくつかあります。あなたはキャスト(またはこの場合は、id)オブジェクトにアクセスするには、ドットシンタックスを使用することはできませんので、INIT、initWithFrameとinitWithCoder

0
[myarray objectAtIndex:i].frame = CGRectMake(i* 110+25, 60, 100, 100);// error, cant build 

はあなたにエラーを与えています。あなたはこの行を削除し、次のように置く場合は、bを設定した後:

b.frame = CGRectMake(i* 110+25, 60, 100, 100); 

あなたは、もはやエラーを取得しません。

また、高速列挙を使用してループを最適化することができます:

for (UIButton *b in myArray) 

は、他の回答者は、あなたが最初の場所で適切にボタンを作成していないことを指摘して正しいです - buttonWithTypeまたはallocinitWithFrameを使用しています。

0
UIButton *btn1 = [[UIButton alloc] init]; 
    UIButton *btn2 = [[UIButton alloc] init]; 
    UIButton *btn3 = [[UIButton alloc] init]; 
    UIButton *btn4 = [[UIButton alloc] init]; 
    UIButton *btn5 = [[UIButton alloc] init]; 
    UIButton *btn6 = [[UIButton alloc] init]; 
    NSArray *myarray = [NSArray arrayWithObjects:btn1, btn2, btn3, btn4, btn5, btn6,nil]; 

    for (int i =0; i < [myarray count]; i++) 
    { 
     NSLog(@"What: %@", [myarray objectAtIndex:i]);   
     UIButton *b = (UIButton *)[myarray objectAtIndex:i]; 
     b.frame = CGRectMake(i* 110+25, 60, 100, 100); 
     [b setTitle:@"my button" forState:UIControlStateNormal]; 
     b.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
     b.titleLabel.textAlignment = UITextAlignmentCenter; 
     float red = 0.33; 
     float green = 0.82; 
     float blue = 0.48; 

     //Color values are float values between 0 and 1; 

     b.titleLabel.textColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0]; 
     b.tag = i+1; 
     [b addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown]; 
     [self.view addSubview: b]; 
     [b release]; 
    }