UIAlertView按钮动作?

时间:2012-04-21 01:36:49

标签: ios objective-c delegates uialertview

我有一个显示此代码的UIAlertView,要求您对appstore中的应用程序进行评分。

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

但我无法弄清楚如何将操作添加到“确定”按钮,该按钮会将您带到 AppStore 中的应用。

3 个答案:

答案 0 :(得分:24)

这个怎么样?

- (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"]];
    } 
}

答案 1 :(得分: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"]];

答案 2 :(得分:0)

swift中的

:使用此代码块显示警告消息。

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)