警告对话框
什么是警告/提示对话框?如下
在手机的使用时经常会出现这样的警告/提示对话框,显示提示或提出警告
警告对话框的定义及使用
首先,在ViewVontroller.h
文件中定义如下的成员变量,及声明如下属性
@interface ViewController : UIViewController<UIAlertViewDelegate> {
//定义一个警告对话框视图对象
UIAlertController* _alert;
}
@property (retain, nonatomic)UIAlertController* alert;
@end
在ViewVontroller.m
文件中进行对警告对话框的定义及使用
先实现属性同步
@implementation ViewController
@synthesize alert = _alert;
@synthesize activityIndicatorView = _activityIndicator;
要想出现一个警告对话框,我们需要有一个按钮UIButton
来触发这个警告对话框
因此先定义按钮UIButton
如下
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 100 + 100 * i, 100, 40);
[btn setTitle:@"警告对话框" forState:UIControlStateNormal];
并用UIButton
的事件响应函数,添加到视图控制器中
[btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
关于pressBtn
中,警告对话框的创建
_alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你的手机电量过低,即将关机,请保存好数据!" preferredStyle:UIAlertControllerStyleAlert];
这里首先使用
UIAlertController
创建一个提示对话框,按照工厂方法即可快速创建,参数UIAlertControllerStyle
只有一种样式:UIAlertControllerStyleAlert
。
然后是警告对话框中关于按键信息的显示
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
UIAlertAction *action2= [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
[_alert addAction:action1];
[_alert addAction:action2];
//[_alert addAction:action3];
//显示对话框
[self presentViewController:_alert animated:YES completion:nil];
- 填写完提示的标题和内容后,就可以使用
UIAlertAction
创建一个具体的按钮行为了。参数UIAlertActionStyle有三种样式:
UIAlertActionStyleDefault
(普通)UIAlertActionStyleCancel
(取消)UIAlertActionStyleDestructive
(警告(破坏性的))。
- 默认状态是正常蓝色字体,取消状态时字体加粗,而警告状态字体则会变为红色。当只添加一个行为对象时,行为对象会显示在
UIAlertController
对象的下面,添加2个时,并排显示,添加3个及以上者,按列显示。
此处注意的是,这种警告对话框的定义及使用的方法适用于iOS 9.0之后的版本,而在iOS 9.0之前的版本中,定义如下
_aleriView = [[UIAlertView alloc] initWithTitle:@"警告"
message:@"你的手机电量过低,即将关机,请保存好数据!"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定" ,nil];
// 显示对话框
[_aleriView show];
运行结果如下:
等待提示器
什么是警告/提示对话框?如下
类似于加载中的图标
等待提示器的定义及使用
首先,在ViewVontroller.h
文件中定义如下的成员变量,及声明如下属性
@interface ViewController : UIViewController<UIAlertViewDelegate> {
//等待提示对象
//当下载,或加载比较大的文件时,可以显示此控件,处于等待提示状态
UIActivityIndicatorView* _activityIndicator;
}
@property (retain, nonatomic)UIActivityIndicatorView* activityIndicatorView;
@end
在ViewVontroller.m
文件中进行对警告对话框的定义及使用
先实现属性同步
@synthesize activityIndicatorView = _activityIndicator;
与刚才的警告提示框类似,都需要一个按钮UIButton
来触发等待提示器
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 100 + 100 * i, 100, 40);
[btn setTitle:@"等待提示器" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
上述pressBtn
函数中包括了等待提示器的定义及使用
- (void)pressBtn {
//宽度和高度不可变
_activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 300, 80, 80)];
//设定提示的风格:小灰,小白,大白
_activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleLarge;
[self.view addSubview:_activityIndicator];
//启动动画并显示
self.view.backgroundColor = [UIColor whiteColor];
_activityIndicator.frame = CGRectMake(150, 400, 100, 100);
[_activityIndicator startAnimating];
//停止动画并隐藏
//[_activityIndicator stopAnimating];
}
启动程序后如下所示
需要注意的是:
- 定义等待提示器的frame(位置宽高)时,宽度和高度是不可变的,无论宽高设置的是多少都不变,它的大小都不变
- 如果想改变其大小(就是上图转圈圈图标的大小),需要用到
等待提示器.activityIndicatorViewStyle = ?
其中,?
有两种形式: -
UIActivityIndicatorViewStyleLarge
大图标
-
UIActivityIndicatorViewStyleMedium
小图标
- 启动动画并隐藏用到方法:
[等待提示器对象 startAnimating]
- 停止动画并隐藏用到方法:
[等待提示器对象 stopAnimating]