美文网首页
iOS9.0后警告框的使用

iOS9.0后警告框的使用

作者: 风飞燕 | 来源:发表于2016-04-21 13:55 被阅读148次

在从iOS8到iOS9的升级过程中,弹出提示框的方式有了很大的改变,在Xcode7 ,iOS9.0的SDK中,已经明确提示不再推荐使用UIAlertView,而只能使用UIAlertController,我们通过代码来演示一下。

我通过点击一个按钮,然后弹出提示框,代码示例如下:

[objc] view plaincopyprint?

import "ViewController.h"

@interface ViewController ()

@property(strong,nonatomic) UIButton *button;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, [[UIScreen mainScreen] bounds].size.width, 20)];
[self.button setTitle:@"跳转" forState:UIControlStateNormal];
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:self.button];

[self.button addTarget:self action:@selector(clickMe:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)clickMe:(id)sender{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"按钮被点击了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];
[alert show];

}

@end

编写上述代码时,会有下列的警告提示:

“‘UIAlertView’ is deprecated:first deprecated in iOS 9.0 - UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead”.
说明UIAlertView首先在iOS9中被弃用(不推荐)使用。让我们去用UIAlertController。但是运行程序,发现代码还是可以成功运行,不会出现crash。

但是在实际的工程开发中,我们有这样一个“潜规则”:要把每一个警告(warning)当做错误(error)。所以为了顺应苹果的潮流,我们来解决这个warning,使用UIAlertController来解决这个问题。代码如下:

[objc] view plaincopyprint?

import "ViewController.h"

@interface ViewController ()

@property(strong,nonatomic) UIButton *button;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, [[UIScreen mainScreen] bounds].size.width, 20)];
[self.button setTitle:@"跳转" forState:UIControlStateNormal];
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:self.button];

[self.button addTarget:self action:@selector(clickMe:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)clickMe:(id)sender{

//初始化提示框;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"按钮被点击了" preferredStyle: UIAlertControllerStyleAlert];

[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//点击按钮的响应事件;
}]];

//弹出提示框;
[self presentViewController:alert animated:true completion:nil];

}

@end

这样,代码就不会有警告了。

程序运行后的效果同上。 其中preferredStyle这个参数还有另一个选择:UIAlertControllerStyleActionSheet。选择这个枚举类型后,实现效果如下:

发现这个提示框是从底部弹出的。是不是很简单呢?通过查看代码还可以发现,在提示框中的按钮响应不再需要delegate委托来实现了。直接使用addAction就可以在一个block中实现按钮点击,非常方便。
虽然用着还不是很习惯,但是还是很好用的,值得收藏

相关文章

  • iOS9.0后警告框的使用

    在从iOS8到iOS9的升级过程中,弹出提示框的方式有了很大的改变,在Xcode7 ,iOS9.0的SDK中,已经...

  • 警告对话框和等待提示器

    警告对话框和等待提示器 警告对话框和等待提示器控件定义(UIAlertView从iOS9.0起弃用) 创建警告对话...

  • Bootstrap警告提示框、徽章、面包屑导航样式

    警告框样式 使用.alert设置警告框基础样式,并使用.alert-success等设置警告框颜色; 在使用了警告...

  • 弹出框(警告框)iOS9.0使用方法

    在iOS9.0之前,使用弹出框的方法就是 之后要具体实现点击某一个按钮时发生的事件,就要在代理方法里面写。 这是之...

  • 第九谈:警告提示框

    本节课我们来开始学习 Bootstrap 的警告框样式以及组件的使用。 一.警告框样式 使用.alert 设置警告...

  • [iOS学习]警告框和操作表

    警告框 iOS中警告框给用户警告或提示,最多两个按钮,超过两个就应该使用操作表。由于在iOS中警告框是模态的,因此...

  • iOS UIAlertController学习笔记

    在最近学习的iOS开发中用到了一些警告提示框,看到的很多教程里用的还是UIAlertview,但是iOS9.0开始...

  • XXAlertView使用

    XXAlertView对UIAlertController封装,能够方便的实现警告框。 使用方法 弹出提示框 弹出...

  • JavaScript 弹窗

    警告框 警告框经常用于确保用户可以得到某些信息。当警告框出现后,用户需要点击确定按钮才能继续进行操作。window...

  • JavaScript 弹窗

    可以在 JavaScript 中创建三种消息框:警告框、确认框、提示框。 警告框 警告框经常用于确保用户可以得到某...

网友评论

      本文标题:iOS9.0后警告框的使用

      本文链接:https://www.haomeiwen.com/subject/ofutrttx.html