NS_CLASS_AVAILABLE_IOS(2_0) @interface UIAlertView : UIView {
從UIAlertView的定義可以看出UIAlertView也是UIView的子類,並且定義在IOS2.0以上的版本。
下面對其定義進行說明,然后舉一個例子說明常用用法:
- (id)initWithTitle:(NSString *)title
message:(NSString *)message
delegate:(id /*<UIAlertViewDelegate>*/)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ... ,nil;
初始化一個UIAlertView,並且指定標題(title)、顯示的文字(message)、委托對象(需要實現UIAlertViewDelegate協議。一般為self,所以這個ViewController對象需要實現這個協議)、取消按鈕的文字(cancelButtonTitle)、其它按鈕的顯示文字(多個的話使用逗號分隔開,以nil結尾)
@property(nonatomic,assign) id/*<UIAlertViewDelegate>*/ delegate; // 委托對象
@property(nonatomic,copy) NSString *title; // 標題文字
@property(nonatomic,copy) NSString *message; // 顯示的消息文本文字
- (NSInteger)addButtonWithTitle:(NSString *)title;
添加一個Button到AlertView並且指定按鈕顯示的文字,並且返回它的索引(從0開始,cancelButton的索引是0)
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
返回指定索引值的按鈕的顯示文本
@property(nonatomic,readonly) NSInteger numberOfButtons;
返回NSAlertView中的所有的按鈕的數量
@property(nonatomic) NSInteger cancelButtonIndex;
@property(nonatomic,readonly) NSInteger firstOtherButtonIndex;
@property(nonatomic,readonly,getter=isVisible) BOOL visible;
- (void)show;
顯示AlertView
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
隱藏按下指定索引值的按鈕之后,隱藏AlertView,並制定是否啟動動畫效果
@property(nonatomic,assign) UIAlertViewStyle alertViewStyle NS_AVAILABLE_IOS(5_0);
- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex NS_AVAILABLE_IOS(5_0);
返回指定索引值的TextField ,這個API僅存在於IOS5.0以上
------------------------------------------------------------------------------------------------------------------------------------------------
下面說一下UIAlertViewDelegate協議,這個協議實現了NSObject非正式協議,沒有必須實現的方法,所有的方法都是可選的
@protocol UIAlertViewDelegate <NSObject>
@optional
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
當一個指定索引的按鈕被點擊的時候,回調此方法,buttonIndex是按鈕的索引值,從0開始
// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)alertViewCancel:(UIAlertView *)alertView;
當用戶按下HOME鍵的時候,回調此方法,用戶點擊Cancel按鈕的時候不會回調此方法
- (void)willPresentAlertView:(UIAlertView *)alertView;
開始顯示View的動畫之前進行回調
- (void)didPresentAlertView:(UIAlertView *)alertView;
顯示動畫完成之后進行回調
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;
將要開始View隱藏動畫的時候進行回調
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
當View的隱藏動畫結束的時候進行回調
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView;
編輯任何默認的字段添加的風格之后調用
例子,這個例子在點擊按鈕的時候,修改UILabel的文字,並且彈出對話框
#import <UIKit/UIKit.h> @interface XinYeViewController : UIViewController <UITextFieldDelegate,UIAlertViewDelegate> @property (weak, nonatomic) IBOutlet UIButton *myButton; @property (weak, nonatomic) IBOutlet UITextField *myTextField; @property (weak, nonatomic) IBOutlet UILabel *myLabel; - (IBAction)clickMyButton:(id)sender; @end
#import "XinYeViewController.h" @interface XinYeViewController () @end // 點擊按鈕的次數 static int CLICK_TIMES = 1; @implementation XinYeViewController @synthesize myButton; @synthesize myLabel; @synthesize myTextField; - (void)viewDidLoad { [super viewDidLoad]; [myTextField becomeFirstResponder]; // 一旦進入見面就顯示軟鍵盤(myTextField獲得焦點) } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (IBAction)clickMyButton:(id)sender { CLICK_TIMES += 1; if(CLICK_TIMES % 2 == 0){ [myLabel setText:@"修改后的文字,modify by xinye"]; }else{ [myLabel setText:@"不要再點了,你煩不煩啊!!!"]; } [[[UIAlertView alloc] initWithTitle:@"這里是標題" message:@"這里顯示的是Message信息" delegate:self cancelButtonTitle:@"Cancel按鈕" otherButtonTitles:@"OK",@"Hello",@"World", nil] show]; } // 在這里處理UIAlertView中的按鈕被單擊的事件 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"buttonIndex is : %i",buttonIndex); switch (buttonIndex) { case 0:{ }break; case 1:{ }break; case 2:{ }break; case 3:{ }break; default: break; } } // 當點擊軟鍵盤的Enter鍵的時候進行回調 -(BOOL)textFieldShouldReturn:(UITextField *)textField { [myTextField resignFirstResponder]; // 讓myTextField失去焦點 return YES; } @end
運行效果如下: