当前位置: 移动技术网 > 移动技术>移动开发>IOS > UIAlertController基本使用

UIAlertController基本使用

2018年12月27日  | 移动技术网移动技术  | 我要评论

从ios8之后,的弹框 uialertview 与 uiactionsheet 两个并在一了起, 使用了一个新的控制器叫 uialertcontroller

uialertcontroller的基本使用

创建uialertcontroller

title:显示的标题

message:标题底部显示的描述信息

preferredstyle:弹框的样式

样式分为两种:

uialertcontrollerstyleactionsheet:

uialertcontrollerstylealert

两种样式分别显示如下:

第一步:创建控制器

uialertcontroller *alertcontroller = [uialertcontroller alertcontrollerwithtitle:@"确定要退出嘛?" message:@“显示的信息" preferredstyle:uialertcontrollerstyleactionsheet];

第二步:创建按钮

弹框当中的每一个按钮分别对应一个 uialertaction

uialertaction创建方式如下:

actionwithtitle:按钮要显示的文字

style:按钮要显示的样式

样式分为三种:

uialertactionstyledefault:默认样式,默认按钮颜色为蓝色

uialertactionstylecancel:设置按钮为取消.点击取消是,会动退出弹框.

注意:取消样式只能设置一个,如果有多个取消样式,则会发生错误.

uialertactionstyledestructive:危险操作按钮,按钮颜色显示为红公

handler:点击按钮时调用block内部代码.

uialertaction *action = [uialertaction actionwithtitle:@"取消" style:uialertactionstylecancel handler:^(uialertaction * _nonnull action) {

nslog(@"点击了取消");

}];

uialertaction *action1 = [uialertaction actionwithtitle:@"确定" style:uialertactionstyledestructive handler:^(uialertaction * _nonnull action) {

nslog(@"点击了取消");

[self.navigationcontroller popviewcontrolleranimated:yes];

}];

第三步:添加按钮

把创建的uialertaction添加到控制器当中.

[alertcontroller addaction:action];

[alertcontroller addaction:action1];

除了添加按钮之外,还可以添加文本框,

添加文本框的前提是uialertcontroller的样式必须得要是uialertcontrollerstylealert样式.否则会直接报错

添加文本框方法为:

[alertcontroller addtextfieldwithconfigurationhandler:^(uitextfield * _nonnull textfield) {

textfield.placeholder = @“文本框点位文字";

}];

运行效果为:

通过控制器的textfields属性获取添加的文本框.注意textfields它是一个数组.

uitextfield *textf = alertcontroller.textfields.lastobject;

第四步:显示弹框.(相当于show操作)

[self presentviewcontroller:alertcontroller animated:yes completion:nil];

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网