ios UIAlertView 按钮动作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10255455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:58:48  来源:igfitidea点击:

UIAlertView button action?

iosobjective-cdelegatesuialertview

提问by JohnAnge Kernodle

I have an UIAlertViewthat shows with this code that asks you to rate the application in the appstore.

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

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

But I cannot figure out how to add an action to the OK button that takes you to the app in the AppStore.

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

回答by CodaFi

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

回答by Domness

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.

当您按下按钮并在您想要调试/测试任何东西时提供帮助时,NSLog 会出现在控制台中。

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

然后对于您想要的操作,您可以编写如下内容:

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

回答by Mr. Tann

in swift : use this block of code to show alert message.

在 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)