OC_UIAlertView与UIActionSheet

327 阅读3分钟

UIAlertViewUIActionSheet 是两个用来展示提示、警告信息的空间,他们现在已经被苹果弃用,被新的 UIAlertController 代替,但是还是有必要进行简单的学习。

UIAlertView

对象创建

UIAlertView 在继承于 UIView 的创建方法外还新增了一个新方法

 (instancetype)initWithTitle:(nullable NSString *)title 
                message:(nullable NSString *)message 
                delegate:(nullable id )delegate 
                cancelButtonTitle:(nullable NSString *)cancelButtonTitle otherButtonTitles:(nullable NSString *)otherButtonTitles, ... );

title 表示提示信息的标题, message 为内容,delegate 表示实现代理的对象,cancelButtonTitle 是取消按钮的标题,以及如果要添加其他按钮即可输入一个或多个 otherButtonTitles ,并以 nil 结尾。

这里会把创建的按钮添加到一个数组中,其中取消按钮的 index 为 0 ,添加的其他按钮的 index 为从 1 开始的序号。

当创建完对象,只需要调用其对象方法即可实现提示的弹出,如:

[alertView show];

UIAlertView 属性

属性 类型 解释
delegate id 设置代理对象
title NSString 提示的标题
message NSString 提示的信息
numberOfButtons NSInteger 只读,按钮的个数(包括取消按钮)
cancelButtonIndex NSInteger 取消按钮的 index
firstOtherButtonIndex NSInteger 只读,第一个其他按钮的 index
visible BOOL 只读,是否可见
alertViewStyle UIAlertViewStyle UIAlertView的风格
  • UIAlertViewStyleDefault : 默认风格
  • UIAlertViewStyleSecureTextInput : 带安全输入框的
  • UIAlertViewStylePlainTextInput : 带普通输入框的
  • UIAlertViewStyleLoginAndPasswordInput : 带账户密码输入框的

UIAlertView 方法

  • 显示 UIAlertView

- (void)show;

  • 添加指定标题的按钮

- (NSInteger)addButtonWithTitle:(nullable NSString *)title;

  • 获取指定 index 位置的按钮的标题

- (nullable NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;

  • 按下指定 index 位置的按钮式隐藏,并指定是否显示动画

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

  • 返回 index 位置的 TextField

- (nullable UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex

UIAlertViewDelegate

  • 点击按钮时调用, buttonIndex 为点击的按钮索引

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

  • 点击取消按钮时调用

- (void)alertViewCancel:(UIAlertView *)alertView;

  • 将要出现时调用

- (void)willPresentAlertView:(UIAlertView *)alertView;

  • 出现时调用

- (void)didPresentAlertView:(UIAlertView *)alertView;

  • 将要消失时调用

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex

  • 消失完成时调用

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

  • 设置第一个其他按钮是否可用

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView

UIActionSheet

对象创建

同样,UIActionSheet 也有属于自己的创建方法:

- (instancetype)initWithTitle:(nullable NSString *)title 
                     delegate:(nullable id<UIActionSheetDelegate>)delegate 
            cancelButtonTitle:(nullable NSString *)cancelButtonTitle
       destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle   
            otherButtonTitles:(nullable NSString *)otherButtonTitles, ...);

UIAlertView 不同的是他多了一个 destructiveButton ,这个按钮是处于 title 下方的第一个红色字体按钮。

还有一个不同的是这里的 destructiveButtonindex 为 0 ,otherButtonindex 为从 1 开始的编号,而 cancelButtonindexotherButton 的个数 +1。

而如果不设置 destructiveButton ,即将 destructiveButtonTitle 设置为 nilotherButtonindex 就变为从 0 开始,从这里可以看出按钮的 index 根据添加的顺序而定。

UIActionSheet 对象出现的方式是调用方法:

- (void)showInView:(UIView *)view;

UIActionSheet 属性

属性 类型 解释
delegate id 设置代理对象
title NSString 提示的标题
actionSheetStyle UIActionSheetStyle UIActionSheet的风格
numberOfButtons NSInteger 只读,按钮的个数(包括取消按钮)
cancelButtonIndex NSInteger 取消按钮的 index
firstOtherButtonIndex NSInteger 只读,第一个其他按钮的 index
destructiveButtonIndex NSInteger 红色按钮的 index
visible BOOL 只读,是否可见

UIActionSheetStyle 内包含以下内容:

typedef NS_ENUM(NSInteger, UIActionSheetStyle) {
    UIActionSheetStyleAutomatic        = -1,                            //自动
    UIActionSheetStyleDefault          = UIBarStyleDefault,             //默认
    UIActionSheetStyleBlackTranslucent = UIBarStyleBlackTranslucent,    //背景变黑
    UIActionSheetStyleBlackOpaque      = UIBarStyleBlackOpaque ,        //同样变黑 
    //这两种其实都是 UIBarStyleBlack
}

UIActionSheet 方法

  • 在指定的视图上显示 UIActionSheet

- (void)showInView:(UIView *)view;

  • 添加指定标题的按钮

- (NSInteger)addButtonWithTitle:(nullable NSString *)title;

  • 获取指定 index 位置的按钮的标题

- (nullable NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;

  • 按下指定 index 位置的按钮式隐藏,并指定是否显示动画

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

  • 在其他的视图上显示的方法

- (void)showFromToolbar:(UIToolbar *)view;

- (void)showFromTabBar:(UITabBar *)view;

- (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated ;

- (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated ;

UIActionSheetDelegate

  • 点击按钮时触发, buttonIndex 为按钮索引

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;

  • 将要弹出时触发的方法

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet;

  • 已经弹出时触发的方法

- (void)didPresentActionSheet:(UIActionSheet *)actionSheet;

  • 将要消失时触发的方法, buttonIndex 为按钮索引

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex;

  • 视图已经消失时触发的方法, buttonIndex 为按钮索引

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;