UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View" message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; ios

標準的雙按鈕,cancel那個buttonIndex 爲0, ok button 的buttonIndex爲1 函數
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@「ThirdButton」, nil];

和程序裏的順序同樣,cancel ok thirdButton 的buttonIndex 分別爲0 1 2
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@「ThirdButton」, nil];
動畫
同理,cancel ok thirdButton FourthButton的buttonIndex 分別爲0 1 2 3 加密
[alertView show]; spa
UIAlertView Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
alertView--->這個不用多說了吧
buttonIndex---->從0開始
能夠經過if (buttonIndex == 1) { } 這樣的來控制點擊了某個按鈕須要作什麼操做
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
這個方法在動畫結束和視圖隱藏以後調用
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
這個方法在動畫開始和視圖隱藏以前調用
- (void)alertViewCancel:(UIAlertView *)alertView
在視圖將要被取消以前
例如,用戶點擊了home鍵
三個函數的調用順序依次是:
alertViewCancel----》willDismissWithButtonIndex---》didDismissWithButtonIndex
- (BOOL)alertViewShouldEnableFirstOtherButton(UIAlertView *)alertView
ios 5+
設置yes / no 將會設置alertView 的第一個otherButton的enable屬性
- (void)didPresentAlertView:(UIAlertView *)alertView
在視圖提交給用戶之後調用
- (void)willPresentAlertView:(UIAlertView *)alertView
在視圖提交給用戶之前調用
這六個delegate 方法調用的順序依次是
alertViewShouldEnableFirstOtherButton---->willPresentAlertView--->didPresentAlertView
---->clickedButtonAtIndex---->(若是會觸發視圖取消,則會調用alertViewCancel)willDismissWithButtonIndex---->didDismissWithButtonIndex
ios4.0之後 alertView不會自動隨着程序轉向後臺而移除
alertView屬性
1.alertViewStyle:
UIAlertViewStyleDefault 只彈信息和按鈕
UIAlertViewStyleSecureTextInput 有一個textfield加密框
UIAlertViewStylePlainTextInput 有一個不加密的textfield
UIAlertViewStyleLoginAndPasswordInput 有兩個textfield,Login和password
只要有textfield就能夠用textfieldAtIndex來捕獲並進行相應的操做例如換鍵盤類型
2.cancelButtonIndex
開始是0,若是沒有設置cancel button 則是-1
3.delegate
若是沒有設置則是nil
4.firstOtherButtonIndex
從0開始,若是沒設置則是-1,並且沒被設置則會被忽略
5.message
消息
6.numberOfButtons
只讀 alertView中的按鈕數量
7.title
標題
8.visible
只讀 若是是yes 表示被顯示
實例方法
- (NSInteger)addButtonWithTitle:(NSString *)title
返回值是增長的Button的index
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex
輸入buttonIndex 返回button的標題
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
程序自動完成點擊buttonIndex的button 並dismiss 整個alertView的操做
- (id)initWithTitle:(NSString *)title message:(NSString)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitles, ...
這個就不用多說了
- (void)show
要顯示必需要調用這個alertview纔會顯示
- (UITextField *)textfieldAtIndex:(NSInteger)textfieldIndex
返回值是textfield
UIAlertViewStyleDefault 沒有
UIAlertViewStyleSecureInput textfieldIndex 只有一個爲0
UIAlertViewStylePlainInput textfieldIndex 只有一個爲0
UIAlertViewStyleLoginAndPasswordInput textfieldIndex有兩個 0 1