替代 iOS 9 的 UIAlertView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32957926/
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
Alternative to UIAlertView for iOS 9?
提问by user3138007
UAlertView
is deprecated in iOS 9 and later. What would be an alternative?
UAlertView
在 iOS 9 及更高版本中已弃用。什么是替代方案?
UIAlertView *new = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[new show];
回答by Nithinbemitk
You can use this code to replace an alert view:
您可以使用此代码替换警报视图:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
If you need multiple actions you can use:
如果您需要多个操作,您可以使用:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"Button 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// action 1
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"Button 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// action 2
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self dismissViewControllerAnimated:YES completion:nil];
}]];
[self presentViewController:alertController animated:YES completion:nil];
回答by vadian
You get often detailed information including the replacement suggestion by ?-clicking on the symbol which displays the class/method declaration.
通过?单击显示类/方法声明的符号,您通常可以获得包括替换建议在内的详细信息。
In case of UIAlertView
you will see
如果UIAlertView
你会看到
"UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead"
“不推荐使用 UIAlertView。使用 UIAlertController 和首选样式 UIAlertControllerStyleAlert 代替”
回答by Mr.Javed Multani
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Info"
message:@"You are using UIAlertController"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
回答by Rémy Blanc
I made a category for that:
我为此做了一个分类:
+ (void)alertViewWithTitle:(NSString *)aTitle message:(NSString *)aMessage viewController:(UIViewController *) aVC
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:aTitle ? aTitle : @""
message:aMessage
preferredStyle:UIAlertControllerStyleAlert];
UIViewController *topVC = aVC ? aVC : [UIApplication sharedApplication].keyWindow.rootViewController;
[topVC presentViewController:alert animated:YES completion:nil];
}
The parameters aTitle
and aVC
are optional but aVC
should be used if known.
参数aTitle
和aVC
是可选的,但aVC
如果已知则应使用。
PS: avoid the "new" as a variable name this is a reserved word, I actually don't know if it will compile though.
PS:避免将“new”作为变量名,这是一个保留字,我实际上不知道它是否会编译。
回答by AmyNguyen
I have use "UIAlertController" on iOS 8 and later. Let see:
我在 iOS 8 及更高版本上使用了“UIAlertController”。让我们看看:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" preferredStyle:UIAlertControllerStyleAlert];
And add buttons:
并添加按钮:
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//do something when click button
}];
Remember:
记住:
[alertController addAction:okAction];
Then show it:
然后显示它:
[self presentViewController:alertController animated:YES completion:nill];
If you want to show a actionsheep, you change
如果你想显示一个actionsheep,你改变
"preferredStyle:UIAlertControllerStyleActionSheet"
回答by quark
UIAlertControllerhas been around since iOS 8.
UIAlertController从 iOS 8开始就已经存在了。
回答by user3138007
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"My Title"
message:@"Enter User Credentials"
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];