OC_UIAlertView与UIActionSheet

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 属性

属性类型解释
delegateid设置代理对象
titleNSString提示的标题
messageNSString提示的信息
numberOfButtonsNSInteger只读,按钮的个数(包括取消按钮)
cancelButtonIndexNSInteger取消按钮的 index
firstOtherButtonIndexNSInteger只读,第一个其他按钮的 index
visibleBOOL只读,是否可见
alertViewStyleUIAlertViewStyleUIAlertView的风格
  • 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 属性

属性类型解释
delegateid设置代理对象
titleNSString提示的标题
actionSheetStyleUIActionSheetStyleUIActionSheet的风格
numberOfButtonsNSInteger只读,按钮的个数(包括取消按钮)
cancelButtonIndexNSInteger取消按钮的 index
firstOtherButtonIndexNSInteger只读,第一个其他按钮的 index
destructiveButtonIndexNSInteger红色按钮的 index
visibleBOOL只读,是否可见

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值