2012-04-21 11 views
9

私はUIAlertViewを持っています。このコードでは、あなたがアプリケーションストアでアプリケーションを評価するよう指示しています。UIAlertViewボタンアクション?

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Rate on the Appstore!" 
               message:@"" 
               delegate:self 
             cancelButtonTitle:@"Later" 
             otherButtonTitles:@"OK", nil]; 
[alert show]; 
[alert release]; 

しかし、私はAppStoreのでアプリが表示されます[OK]ボタンにアクションを追加する方法を見つけ出すことはできません。

答えて

25

次に、あなたが望む行動のために、あなたのような何かを書くだろうか?

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex != [alertView cancelButtonIndex]) { 
     NSLog(@"Launching the store"); 
     //replace appname with any specific name you want 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.com/apps/appname"]]; 
    } 
} 
+0

メソッド名をclickedButtonAtIndexに変更してください...上記の同じメソッド名とメソッドが呼び出されない場所をコピーする可能性があります。 –

+0

ViewController.hのちょっとした点、次を追加してください: @interfaceの後ViewController:UIViewController so次のようになります: "@interface ViewController:UIViewController " – JomanJi

+0

だからどこでこのメソッドを呼び出すのですか? ヘッダーファイルにUIViewController を追加したか、[self alertView ...]のようなことをする必要があるので、それは自分自身で呼び出されますか? – SteBra

9

あなたは、次のような何かをしたい:ボタンを押すと、デバッグ/テスト用のものにしたい時はいつでも手伝うとき

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     NSLog(@"Clicked button index 0"); 
     // Add the action here 
    } else { 
     NSLog(@"Clicked button index other than 0"); 
     // Add another action here 
    } 
} 

のNSLogのは、コンソールに表示されます。これはどのように

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"url_to_app_store"]]; 
+0

、代わりにチェックのボタンインデックスが '0'である場合は、キャンセルボタンが ''(buttonIndex!= [alertView cancelButtonIndex])場合は押されたりされなかった場合。チェックしたいでしょう – chown

+0

他のボタンが押された場合には、今後のチェックのために追加のヘルプを追加していました。 – Domness

+0

メソッド名をclickedButtonAtIndexに変更してください....上記の同じメソッド名をコピーして、メソッドが呼び出されない場所がある可能性があります。 –

0

swift:このコードブロックを使用して警告メッセージを表示します。 2ボタンの警告については

let alert = UIAlertController(title: "Alert", message: "This is an alert message", preferredStyle: UIAlertControllerStyle.Alert) 

let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {(action:UIAlertAction) in print("This is in alert block") 
}) 

alert.addAction(action) 
self.presentViewController(alert, animated: true, completion: nil)