這個方法經過設置一個標題,內容,代理和一些按鈕的標題建立警告框,代碼示例以下:atom
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"個人警告框" message:@"這是一個警告框" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"肯定", nil]; [alert show];
效果以下:spa
注意:若是按鈕數超過兩個,將會建立成以下樣子:.net
若是按鈕數量超出屏幕顯示範圍,則會建立相似tableView的效果。3d
標題屬性代理
@property(nonatomic,copy) NSString *title;code
內容屬性索引
@property(nonatomic,copy) NSString *message;get
添加一個按鈕,返回的是此按鈕的索引值it
- (NSInteger)addButtonWithTitle:(NSString *)title; table
返回根據按鈕索引按鈕標題
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
獲取按鈕數量
@property(nonatomic,readonly) NSInteger numberOfButtons;
設置將某一個按鈕設置爲取消按鈕
@property(nonatomic) NSInteger cancelButtonIndex;
返回其餘類型按鈕第一個的索引值
@property(nonatomic,readonly) NSInteger firstOtherButtonIndex;
警告框是否可見
@property(nonatomic,readonly,getter=isVisible) BOOL visible;
顯現警告框
- (void)show;
代碼模擬點擊按鈕消失觸發方法
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
設置警告框風格
@property(nonatomic,assign) UIAlertViewStyle alertViewStyle;
風格的枚舉以下
typedef NS_ENUM(NSInteger, UIAlertViewStyle) { UIAlertViewStyleDefault = 0,//默認風格 UIAlertViewStyleSecureTextInput,//密碼輸入框風格 UIAlertViewStylePlainTextInput,//普通輸入框風格 UIAlertViewStyleLoginAndPasswordInput//帳號密碼框風格 };
這個方法設置文本輸入框的索引
- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex;
點擊按鈕時觸發的方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
將要展示警告框時觸發的方法
- (void)willPresentAlertView:(UIAlertView *)alertView;
已經展示警告框時觸發的方法
- (void)didPresentAlertView:(UIAlertView *)alertView;
警告框將要消失時觸發的方法
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;
警告框已經消失時觸發的方法
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
設置是否容許第一個按鈕不是取消按鈕
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView;
專一技術,熱愛生活,交流技術,也作朋友。
——琿少 QQ羣:203317592