2011-01-05 10 views
1

I皆、alertViewボタンにアクションを追加すること

私は一つ一つが、「キャンセル」と「OK」と各「OK」ボタンは、別のビューに行くとAlertViewを呼び出す3つのボタンがあります。今、私はすべての3つのボタンのこの

- (UIButton *)1_BTN 
{ 
if (1_BTN == nil) 
{ 
    UIImage *buttonBackground = [UIImage imageNamed:@"1_btn.png"]; 
    UIImage *buttonBackgroundPressed = [UIImage imageNamed:@"1_btn.png"]; 

    CGRect frame = CGRectMake(655, 985, 107, 30); 

    1_BTN = [_IPadAppDelegate buttonWithTitle:@"" 
            target:self 
            selector:@selector(1_BTN:) 
             frame:frame 
             image:buttonBackground 
           imagePressed:buttonBackgroundPressed]; 
[1_BTN setTag:1]; 
} 
return 1_BTN; 
} 
...... 

- (void)1_BTN:(NSInteger *)sender 
{ 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"some fancy text" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"ok", nil]; 

[alert setTag:[sender valueForKey:@"tag"]]; 
[alert show]; 
[alert release]; 
} 

....... 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

if (buttonIndex == 1) { 
if ([[alertView tag] isEqualToNumber:[NSNumber numberWithInt:1]]) { 
something should happen..... 
} 

でこれを解決し、それが正常に動作しますが、

[alert setTag:[sender valueForKey:@"tag"]]; 

if ([[alertView tag] isEqualToNumber:[NSNumber numberWithInt:1]]) { 

のために私はこの警告「無効な受信機を得るため

タイプ "NSInteger" "

それはなぜですか、どうすればこの問題を解決できますか?

+0

コードをレイアウトしてください。あなたの実際のコードのコードマークアップを使用してください。これはあなたの問題を分析する方がはるかに優れています。 – burki

答えて

2

これはまったく間違っています。 UIAlertViewDelegate、具体的にはalertView:clickedButtonAtIndex:を実装し、どのボタンインデックス(0 .. nから、nが最後のボタンであるか)を確認します。したがって、あなたの既知の固定指数に基づいて行動してください。

第2に、NSIntegerはスカラー非オブジェクト型であり、メッセージを受信できません。代わりに均等比較、つまりalertview.tag == 1が必要です。しかし、私が以前に言ったように、そうしないでください。

+0

私は最初にこれを試しましたが、 "OK"ボタンはいつも同じ動作をしていました。なぜなら、タグを試した理由とその方法........ 警告だけが私を悩ます。 – Frank

+0

何かが一方通行であるという理由だけで、あなたはそうするべきではありません。あなたはそれをデリゲートの方法で行い、タップされたボタンのインデックスに基づいて適切な処置を講じます。これがあなたがそれをしなければならない方法です。 – jer

1

UIViewプロパティ "tag"はオブジェクトではなく、単純なNSIntegerです。これは、おそらくC/C++から知っている "int"とほとんど同じです。あなたのコードでいくつかの行を修正しました。今それは動作するはずです。

別のこと:UIButtonの割り当ては、私にとってはちょっと変わったようです。たぶんあなたのメモリ管理をチェックする必要があります。

- (UIButton *)1_BTN 
    { 
     if (1_BTN == nil) { 
      UIImage *buttonBackground = [UIImage imageNamed:@"1_btn.png"]; 
      UIImage *buttonBackgroundPressed = [UIImage imageNamed:@"1_btn.png"]; 

      CGRect frame = CGRectMake(655, 985, 107, 30); 

      1_BTN = [_IPadAppDelegate buttonWithTitle:@"" target:self selector:@selector(1_BTNAction:) frame:frame image:buttonBackground imagePressed:buttonBackgroundPressed]; 
      [1_BTN setTag:1]; 
     } 
     return 1_BTN; 
    } 




    - (void)1_BTNAction:(UIButton *)sender { 
     NSInteger tagNumber = [sender tag]; 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"sone fancy text" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"ok", nil]; 

     [alert setTag:tagNumber]; 
     [alert show]; 
     [alert release]; 
    } 



    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
     if (buttonIndex == 1) { 
      if ([alertView tag] == 1) { 
       //something should happen 
      } 
     } 
    } 
+0

読み込みに少し気をつけていただきましたが、今は同じwarnungやNSInteger tagNumber = [送信者タグ]を取得してくれてありがとうございます。 – Frank

+0

さて、エラーを説明できますか?構文エラーですか、ボタンを押すとアプリケーションが終了しますか? – burki

関連する問題