iOS 9的UIAlertView的替代方案?
iOS 9的UIAlertView的替代方案?,ios,objective-c,ios9,uialertview,uialertcontroller,Ios,Objective C,Ios9,Uialertview,Uialertcontroller,UAlertView在iOS 9及更高版本中不推荐使用。另一种选择是什么 UIAlertView *new = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [new show]; 自iOS 8以来一直存在。您经
UAlertView
在iOS 9及更高版本中不推荐使用。另一种选择是什么
UIAlertView *new = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[new show];
自iOS 8以来一直存在。您经常会收到详细信息,包括用户的更换建议⌘-单击显示类/方法声明的符号
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"My Title"
message:@"Enter User Credentials"
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
如果是UIAlertView
,您将看到
“UIAlertView已弃用。请改用首选样式为UIAlertControllerStyleAlert的UIAlertController”
我在iOS 8和更高版本上使用了“UIAlertController”。让我们看看:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" preferredStyle:UIAlertControllerStyleAlert];
和添加按钮:
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//do something when click button
}];
记住:
[alertController addAction:okAction];
然后展示它:
[self presentViewController:alertController animated:YES completion:nill];
如果你想展示一个动作,你可以改变
"preferredStyle:UIAlertControllerStyleActionSheet"
您可以使用此代码替换警报视图:
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];
如果需要多个操作,可以使用:
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];
我为此做了一个分类:
+ (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];
}
参数aTitle
和aVC
是可选的,但如果已知,则应使用aVC
PS:避免将“new”作为变量名这是一个保留字,但我实际上不知道它是否会编译。有什么原因你没有简单地查看文档以查看
UIAlertView
(它确切地告诉你要做什么)或在发布此问题之前进行搜索?请在发布之前尝试寻找答案。如果您在他们的文档中找不到答案,最好将您的项目转移到iOS 8:这需要很多代码来实现。。。他们在想什么?不过,在没有委托的情况下处理响应的能力是一个很好的补充。@Kundapra Hudga,您为什么选择对[self-presentViewController:alertController-animated:YES completion:nil]使用dispatch_async?如果要使用UIAlertController,请参阅有关Swift AlertController教程:为什么从调度队列调用presentViewController?
+ (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];
}