UI学习——警告对话框与等待提示器


一、警告对话框

1.基本概念

警告对话框是一种常见的用户界面元素,用于向用户显示警告消息并收集用户响应。在 iOS 中,我们可以使用 UIAlertController 和 UIAlertAction 类来创建和管理警告对话框。
UIAlertController 类是一个视图控制器,用于管理警告对话框中的各个元素,包括标题、消息和操作按钮。我们可以使用 alertControllerWithTitle:message:preferredStyle: 方法来创建一个新的 UIAlertController 实例。然后,我们可以使用 addAction: 方法来添加操作按钮,最后使用 presentViewController:animated:completion: 方法来显示警告对话框。

2.示例代码

首先定义一个警告对话框对象

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    //定义警告对话框对象
    UIAlertController* _alertview;
}
@property (retain, nonatomic)UIAlertController* alertview;

@end

我们需要创建一个按钮来触发警告对话框

for (int i = 0; i < 2; i++) {
        UIButton* btn = [[UIButton alloc] init];
        btn.frame = CGRectMake(100, 100 + 100 * i, 100, 40);
        if (i == 0) {
            [btn setTitle:@"警告对话框" forState: UIControlStateNormal];
            [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

        } else if (i == 1) {
            [btn setTitle:@"等待提示器" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        }
        btn.tag = 101 + i;
        [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }

下面实现警告对话框

- (void)pressBtn:(UIButton*) btn {
    //警告对话框创建
    if (btn.tag == 101) {
        _alertview = [UIAlertController alertControllerWithTitle:@"警告" message:@"你的手机电量过低,即将关机,请保存好数据!" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
        UIAlertAction* action02 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
        [_alertview addAction:action01];
        [_alertview addAction:action02];
        [self presentViewController:_alertview animated:YES completion:nil];
    }

alertControllerWithTitleUIAlertController
类的一个方法,用于创建一个新的警告控制器实例。该方法需要传递三个参数,分别是 title、message 和 preferredStyle。
title:警告控制器的标题。它通常是一段简短的文本,用于描述警告的主要内容。
message:警告控制器的消息。它是一个可选的参数,通常包含更详细的说明或上下文信息。
preferredStyle:警告控制器的样式。它是一个枚举类型的值,可以是 UIAlertControllerStyleAlert 或
UIAlertControllerStyleActionSheet。前者表示警告控制器以弹出方式显示,后者表示以滑出方式显示。

UIAlertAction 是iOS中用于表示警告控制器中的一个操作的类。通常,它与 UIAlertController 一起使用,可以向警告控制器添加多个操作按钮。
UIAlertAction 有两个主要属性: title 和 style。 title 表示操作的标题,而 style 表示操作的风格,可以是 UIAlertActionStyleDefault、UIAlertActionStyleCancel 或 UIAlertActionStyleDestructive 之一。
UIAlertActionStyleDefault 表示一个默认的操作,通常使用白色文本和蓝色背景颜色。
UIAlertActionStyleCancel 表示一个取消操作,通常使用粗体字体并在警报控制器中以特殊方式显示。
UIAlertActionStyleDestructive 表示一个具有破坏性的操作,通常使用红色文本。

actionWithTitle是一个iOS开发中的方法,用于创建一个带有标题的动作按钮。它的三个参数分别是:
title:表示按钮的标题,是一个字符串类型的参数。
style:表示按钮的样式,是一个UIAlertActionStyle类型的参数,可以设置为默认、取消或警告样式。
handler:表示按钮被点击后执行的处理程序,是一个可选的闭包类型的参数。当按钮被点击时,该处理程序将被调用以执行相关操作。

运行结果如下:
请添加图片描述

二、等待提示器

1.基本概念

等待提示器是一种常见的用户界面元素,用于告诉用户正在执行某种操作且需要等待一段时间。在 iOS 中,我们可以使用 UIActivityIndicatorView 类来创建和管理等待提示器。
UIActivityIndicatorView 是一个视图类,用于显示旋转的指示器,表示正在进行一些操作。我们可以使用 initWithActivityIndicatorStyle: 方法创建一个新的 UIActivityIndicatorView 实例,并将其添加到视图层次结构中。然后,我们可以使用 startAnimating 和 stopAnimating 方法来启动和停止旋转动画。

2.示例代码

首先定义一个等待提示器对象

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    //定义等待提示器对象
    UIActivityIndicatorView* _activityIndicator;
}
@property (retain, nonatomic)UIActivityIndicatorView* activityIndicator;

@end

我们需要创建一个按钮来触发等待提示器

for (int i = 0; i < 2; i++) {
        UIButton* btn = [[UIButton alloc] init];
        btn.frame = CGRectMake(100, 100 + 100 * i, 100, 40);
        if (i == 0) {
            [btn setTitle:@"警告对话框" forState: UIControlStateNormal];
            [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

        } else if (i == 1) {
            [btn setTitle:@"等待提示器" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        }
        btn.tag = 101 + i;
        [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }

下面实现等待提示器

//等待提示器创建
    else if (btn.tag == 102){
        //宽度和高度不可改变
        _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 300, 80, 80)];
        //设定提示的风格
//        _activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleMedium;
        _activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleLarge;
        [self.view addSubview:_activityIndicator];
        //启动动画并显示
        [_activityIndicator startAnimating];
        //停止动画并隐藏
        [_activityIndicator stopAnimating];
    }
}

需要注意的是,在iOS13之后设定等待提示器的风格只有UIActivityIndicatorViewStyleLarge和UIActivityIndicatorViewStyleMedium两种风格舍弃了原来小白、小灰、大白三种风格。

运行结果如下:
请添加图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值