iOS 8: 三、UIAlertController  VS  UIAlertView and UIActionSheet

UIAlertView和UIActionSheet都是iOS系统自带的弹出式对话框。当UIAlertView或UIActionSheet显示出来时,用户无法与应用界面中的其他控件交互。UIAlertView与UIActionSheet的最大区别在于:UIAlertView表现为显示在屏幕中央的弹出警告框;UIActionSheet则表现为显示在底部的按钮列表;

一、UIAlertView

1、UIAlertView的基本用法

  1. 创建UIAlertView,创建该对象时刻指定该警告框的标题、消息内容,以及该警告框包含多少个按钮等信息。如果程序需要监听用户点击了警告框的哪个按钮,还需要在创建UIAlertView时设置UIAlertViewDelegate委托对象;
  2. 调用UIAlertView显示出来即可;
  3. 如果需要监听用户点击了警告框中的哪个按钮,为委托对象是吸纳UIAlertViewDelegate协议中的方法。

2、UIAlertView的基本属性

  • (1)、- (instancetype)initWithTitle:(NSString )title message:(NSString )message delegate:(id //)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, … NS_REQUIRES_NIL_TERMINATION NS_EXTENSION_UNAVAILABLE_IOS(“Use UIAlertController instead.”);

      初始化
      参数:
      	①、title:标题
      	②、delegate:遵循UIAlertViewDelegate的代理
      	③、message:信息
      	④、cancelButtonTitle:按钮标题
      	⑤、otherButtonTitles:其他按钮标题
    
  • (2)、@property(nonatomic,assign) id // delegate;

      设置代理(遵循UIAlertViewDelegate)
    
  • (3)、@property(nonatomic,copy) NSString *title;

      设置标题
    
  • (4)、@property(nonatomic,copy) NSString *message;

      设置信息
    
  • (5)、- (NSInteger)addButtonWithTitle:(NSString *)title;

      给指定标题添加按钮
    
  • (6)、- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;

      获取指定索引的按钮的标题
    
  • (7)、@property(nonatomic,readonly) NSInteger numberOfButtons;

      获取按钮总数
    
  • (8)、@property(nonatomic) NSInteger cancelButtonIndex;

      获取取消按钮的索引
    
  • (9)、@property(nonatomic,readonly) NSInteger firstOtherButtonIndex;

      获取第一个其他按钮的索引,只读属性
    
  • (10)、@property(nonatomic,readonly,getter=isVisible) BOOL visible;

      获取AlertView是否可见,只读属性
    
  • (11)、- (void)show;

      展示
    
  • (12)、- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

      隐藏,若点击不需调用;
    

##3、UIAlertView的输入框

  • (1)、 @property(nonatomic,assign) UIAlertViewStyle alertViewStyle NS_AVAILABLE_IOS(5_0);

该属性用于设置该UIAlertView的风格,它支持如下枚举值:(UIAlertViewStyle)
UIAlertViewStyleDefault:默认的警告框风格;
UIAlertViewStyleSecureTextInput:警告框中包含一个密码的输入框;
UIAlertViewStylePlainTextInput:警告框中包含一个普通的输入框;
UIAlertViewStyleLoginAndPasswordInput:警告框中包含用户名、密码两个输入框。

例如:

MyAlertView.alertViewStyle = UIAlertViewStyleDefault;

通过该UIAlertView的actionSheerStyle属性,即可控制警告框使用不同的风格,包括带普通文本框、带密码框、带用户名和密码输入框的警告框。

  • (2)、- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex;

如果UIAlertView控件中带有输入框,程序可通过上述方法来访问该警告框中的输入框,获取textFieldIndex索引对应的文本框。第一个文本框的索引为0;通过上述的方式获取UIAlertView中包含的UITextView控件之后,接下来就可以对该UITextField进行任何操作,包括定制其外观(改变它的字体和颜色、关联的键盘灯等)。也可以获取用户在UITextView中输入的字符。

例如:

UITextField* textField = [MyAlertView textFieldAtIndex:0];
  • (3)、当想改变UIAlertView中的message中的UILabel的属性时,可在UIAlertView将要出现的时候遍历其子视图,然后设置属性,例如:
- (void)willPresentAlertView:(UIAlertView *)alertView{
    for (UIView* view in alertView.subviews) {
        if ([view isKindOfClass:[UILabel class]]) {
            UILabel* label = (UILabel *)view;
            label.textAlignment = NSTextAlignmentLeft;
        }
    }
}

3、UIAlertViewDelegate

#pragma mark -UIAlertViewDelegate委托方法
  • (1)、- (void)willPresentAlertView:(UIAlertView *)alertView;

      //当警告框将要显示出来时将会激发该方法;
    
  • (2)、 - (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView;

      //当该警告框中第一个非Cancel按钮被启用时激发该方法;
    
  • (3)、 - (void)didPresentAlertView:(UIAlertView *)alertView;

      //当警告框完全显示出来后将会激发该方法;
    
  • (4)、 - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;

      //当用户通过单击某个按钮将要隐藏该警告框时激发该方法;
    
  • (5)、 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

      //当用户单击该警告框中某个按钮时激发该方法,其中buttonIndex参数代表用户单击的按钮的索引,该索引从0开始;
    
  • (6)、 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;

      //点那个用户用过单击某个按钮完全隐藏该警告框时激发该方法;
    
  • (7)、 - (void)alertViewCancel:(UIAlertView *)alertView;

      //当该对话框被取消时(如用户点击了Home键)激发该方法;
    

这里写图片描述

二、UIActionSheet

1、UIActionSheet的基本用法

UIActionSheet变现为显示底部按钮列表,用户通过单击某个按钮来表明自己的态度,默认情况下,UIActionSheet只支持一个标题和多个按钮,UIActionSheet会有两个固定的按钮和多个其他按钮,两个固定按钮如下:

  • (1)、灰色背景的取消按钮,该按钮用户取消显示该UIActionSheet。
  • (2)、红色背景的销毁按钮,当用户视图删除某个文件或某条记录时,可让用户通过该按钮来确认删除。

2、UIActionSheet的基本属性

  • (1)、 - (instancetype)initWithTitle:(NSString *)title delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, … NS_REQUIRES_NIL_TERMINATION NS_EXTENSION_UNAVAILABLE_IOS(“Use UIAlertController instead.”);

      初始化
      参数:
      	①、title:标题;
      	②、delegate:遵循UIActionSheetDelegate的代理;
      	③、cancelButtonTitle:取消按钮标题;
      	④、destructiveButtonTitle:默认红色按钮的标题
      	⑤、otherButtonTitles:其他按钮的标题
    
  • (2)、 @property(nonatomic,assign) id delegate;

      设置遵循UIActionSheetDelegate的delegate;
    
  • (3)、 @property(nonatomic,copy) NSString *title;

      设置标题;
    
  • (4)、 @property(nonatomic) UIActionSheetStyle actionSheetStyle;

设置控件的风格,该属性支持如下枚举值:
UIActionSheetStyleDefault:默认风格,灰色背景上显示白色文字;
UIActionSheetStyleBlackTranslucent:在透明的黑色背景上显示白色文字;
UIActionSheetStyleBlackOpaque:在纯黑的背景上显示白色文字;

  • (5)、 - (NSInteger)addButtonWithTitle:(NSString *)title;

      通过给指定标题添加按钮;
    
  • (6)、 - (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;

      获取指定索引的标题;
    
  • (7)、 @property(nonatomic,readonly) NSInteger numberOfButtons;

      获取按钮个数;
    
  • (8)、 @property(nonatomic) NSInteger cancelButtonIndex;

      获取取消按钮的索引;
    
  • (9)、 @property(nonatomic) NSInteger destructiveButtonIndex;

      获取红色按钮的索引;
    
  • (10)、 @property(nonatomic,readonly) NSInteger firstOtherButtonIndex;

      获取第一个其他按钮的索引,该属性为只读按钮;
    
  • (11)、 - (void)showFromToolbar:(UIToolbar *)view;

      设置UIActionSheet显示再一个UIToolbar控件上;
    
  • (12)、 - (void)showFromTabBar:(UITabBar *)view;

      设置UIActionSheet显示再一个UITabBar控件上;
    
  • (13)、 - (void)showInView:(UIView *)view;

      设置UIActionSheet显示再一个UIView控件上;
    
  • (14)、 - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

      不考虑指定索引的按钮的动作,可以设置是否有动画;
    

3、UIActionSheetDelegate

#pragma mark -UIActionSheetDelegate委托方法
  • (1)、- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;//当用户单击该警告框中某个按钮时激发该方法,其中buttonIndex参数代表用户单击的按钮的索引,该索引从0开始;
  • (2)、 - (void)actionSheetCancel:(UIActionSheet *)actionSheet;//当该对话框被取消时(如用户点击了Home键)激发该方法;
  • (3)、 - (void)willPresentActionSheet:(UIActionSheet *)actionSheet;//当警告框将要显示出来时将会激发该方法;
  • (4)、- (void)didPresentActionSheet:(UIActionSheet *)actionSheet;//当警告框完全显示出来后将会激发该方法;
  • (5)、- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex;//当用户单击该警告框中某个按钮时激发该方法,其中buttonIndex参数代表用户单击的按钮的索引,该索引从0开始;
  • (6)、- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;//点那个用户用过单击某个按钮完全隐藏该警告框时激发该方法;

这里写图片描述

三、UIAlertController

在iOS 8中,UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两货的功能和作用。是使用对话框(alert)还是使用上拉菜单(action sheet),就取决于在创建控制器时,您是如何设置首选样式的。

1、 UIAlertAction

  • (1)、 + (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(UIAlertAction *action))handler;

      初始化
      参数:
      	①、title:标题
      	②、style:类型(动作样式),包括如下枚举值:
      	typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
      		UIAlertActionStyleDefault = 0,     //常规
      		UIAlertActionStyleCancel,             //取消
      		UIAlertActionStyleDestructive      //警示
      	} NS_ENUM_AVAILABLE_IOS(8_0);
      	③、handler:点击调用block
    
  • (2)、 @property (nonatomic, readonly) NSString *title;

      设置标题;
    
  • (3)、 @property (nonatomic, readonly) UIAlertActionStyle style;

      设置类型;
    
  • (4)、 @property (nonatomic, getter=isEnabled) BOOL enabled;

      是否可点击;
    

2、 UIAlertController

  • (1)、+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;

      初始化
      参数:
      	①、title:标题
      	②、message:信息
      	③、preferredStyle:类型,该值可以是如下枚举值
      	typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
      		UIAlertControllerStyleActionSheet = 0,
      		UIAlertControllerStyleAlert
      	} NS_ENUM_AVAILABLE_IOS(8_0);
    
  • (2)、 - (void)addAction:(UIAlertAction *)action;

      添加UIAlertAction控件;
    
  • (3)、 - (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;

      添加文本对话框
    
  • (4)、 @property (nonatomic, readonly) NSArray *actions;

      该属性返回所有UIAlertAction控件;
    
  • (5)、 @property (nonatomic, copy) NSString *title;

      设置标题;
    
  • (6)、@property (nonatomic, copy) NSString *message;

      设置信息;
    
  • (7)、 @property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;

      设置类型
    

3、对话框(alert)

- (IBAction)alertClick:(id)sender {
    NSLog(@"点击了警告框");
    UIAlertController*al=[UIAlertController alertControllerWithTitle:@"警告框" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [al addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"确定");
    }]];

    [al addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"取消");
    }]];
    [al addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"登录";
    }];
    [al addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"密码";
        textField.secureTextEntry = YES;
    }];
    [self presentViewController:al animated:YES completion:nil];
}

4、上拉菜单(action sheet)

- (IBAction)actionClick:(id)sender {
    NSLog(@"点击了选择框");
    UIAlertController*al=[UIAlertController alertControllerWithTitle:@"选择照片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [al addAction:[UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"相机");
    }]];
    [al addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"取消");
    }]];
    [self presentViewController:al animated:YES completion:nil];
}

注意:每个UIAlertController只允许包含一个UIAlertActionStyleCancel类型的UIAlertAction。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoxiaobukuang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值