陈斌彬的技术博客

Stay foolish,stay hungry

iOS组件之UIAlertView详解

引言

使用UIAlertView类显示一个警告信息给用户。警报视图的功能类似,但不同的外观在一个动作片( UIActionSheet的一个实例)。UIAlertView继承于UIView(有关UIView的介绍请到《iOS组件之UIView详解》)。

使用定义的属性和方法在这个类来设置标题,消息和警报视图的委托和配置按钮。你必须设置一个委托,如果你添加自定义按钮。委托应符合UIAlertViewDelegate协议。使用show方法来显示一个警报视图,一旦它被配置。

目录

// 初始化UIAlertView
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id <UIAlertViewDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... ;
// 设置代理
id <UIAlertViewDelegate> delegate;
// 设置title
NSString *title;
// 显示的消息文本文字
NSString *message;
// 添加一个Button到AlertView并且指定按钮显示的文字,并且返回它的索引(从0开始,cancelButton的索引是0)
- (NSInteger)addButtonWithTitle:(NSString *)title; 
// 通过下标获取button的title
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
// button的总数(只读)
NSInteger numberOfButtons;
// 取消按钮的下标
NSInteger cancelButtonIndex;
// 第一个其他button的下标(只读)
NSInteger firstOtherButtonIndex;
// 是否显示了(只读)
BOOL visible;
// 动画显示alert.
- (void)show;
// 隐藏按下指定索引值的按钮之后,隐藏AlertView,并制定是否启动动画效果
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
// 设置alertView样式(iOS5.0)
UIAlertViewStyle alertViewStyle;
// 返回指定索引值的TextField ,这个API仅存在于IOS5.0以上
- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex;
UIAlertViewDelegate协议(可选)
// 当一个指定索引的按钮被点击的时候,回调此方法,buttonIndex是按钮的索引值,从0开始
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
// 当用户按下HOME键的时候,回调此方法,用户点击Cancel按钮的时候不会回调此方法
- (void)alertViewCancel:(UIAlertView *)alertView;
// 开始显示View的动画之前进行回调
- (void)willPresentAlertView:(UIAlertView *)alertView;  
// 显示动画完成之后进行回调
- (void)didPresentAlertView:(UIAlertView *)alertView;  
// 将要开始View隐藏动画的时候进行回调
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex; 
// 当View的隐藏动画结束的时候进行回调
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
// 编辑任何默认的字段添加的风格之后调用
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView;