【iOS】UI控件学习——警告对话框和等待提示器

警告对话框

什么是警告/提示对话框?如下

在手机的使用时经常会出现这样的警告/提示对话框,显示提示或提出警告

警告对话框的定义及使用

首先,在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有三种样式:
  1. UIAlertActionStyleDefault(普通)
  2. UIAlertActionStyleCancel(取消)
  3. 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 = ?其中,有两种形式:
    1. UIActivityIndicatorViewStyleLarge 大图标
    1. UIActivityIndicatorViewStyleMedium 小图标
  • 启动动画并隐藏用到方法:[等待提示器对象 startAnimating]
  • 停止动画并隐藏用到方法:[等待提示器对象 stopAnimating]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值