UIKit框架-05.UIAlertView使用

来源:互联网 发布:mac尘埃3汉化 编辑:程序博客网 时间:2024/06/06 00:18

1.UIAlertView概述

  • 此前在提示框功能中有介绍到苹果自带的类可以实现弹窗的功能,这里的UIAlertView弹出效果如下:
    这里写图片描述

2.UIAlertView基本使用

  • UIAlertView在屏幕中央弹出
  • UIAlertView可以添加文本输入框,可以实现登陆窗口功能
  • 或者实现提示验证效果
  • 这里我们实现点击屏幕,弹出弹框,点击按钮后弹框消失

  • 代码实现:

// 触摸屏幕事件-(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{    // 1.创建一个UIAlertView,可以在初始化的时候创建多个按钮    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示框" message:@"这是一个UIAlertView提示框" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"Other", nil];    // 2.设置UIAlertView的样式    /*     UIAlertViewStyleDefault = 0,默认样式     UIAlertViewStyleSecureTextInput,一行密文输入框     UIAlertViewStylePlainTextInput,一行普通文本输入框     UIAlertViewStyleLoginAndPasswordInput 两行输入框,一行普通文本输入框,一行密文输入框     */    alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;    // 3.显示到UIView上    [alert show];}/** *  监听按钮的点击事件 */#pragma mark - UIAlertViewDelegate-(void)alertView:(nonnull UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    // 取消按钮索引为0,确定按钮索引是1,其余按钮依次增加    NSLog(@"%ld按钮被点击了",buttonIndex);}
0 0