9

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

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

但我不知道如何向 OK 按钮添加一个操作,将您带到AppStore中的应用程序。

4

3 回答 3

24

How about this?

- (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"]];
    } 
}
于 2012-04-21T01:44:24.320 回答
9

You want something like the following:

- (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's appear in the console when you press a button and help out whenever you want to debug/test anything.

Then for the action that you want, you'd write something like:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"url_to_app_store"]];
于 2012-04-21T01:42:18.713 回答
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)
于 2016-09-08T09:31:26.663 回答