ios UIAlertView使用全解

UIAlertView使用全解

1.

UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"Default AlertView"message:@"Defalut" delegate:selfcancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

 標準的雙按鈕,cancel那個buttonIndex 0 ok button buttonIndex1


 2.

 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


3.

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

 同理,cancel   ok   thirdButton FourthButtonbuttonIndex 分別爲0 1 2 3

[alertView show];

 

4. 手動的取消對話框

 

 [alertdismissWithClickedButtonIndex:0animated:YES];

 

5:爲UIAlertView添加子視圖


 在爲UIAlertView對象太添加子視圖的過程中,有點是需要注意的地方,如果刪除按鈕,也就是取消UIAlerView視圖中所有的按鈕的時候,可能會導致整個顯示結構失衡。按鈕佔用的空間不會消失,我們也可以理解爲這些按鈕沒有真正的刪除,僅僅是他不可見了而已。如果在UIAlertview對象中僅僅用來顯示文本,那麼,可以在消息的開頭添加換行符(@"\n)有助於平衡按鈕底部和頂部的空間。

 下面的代碼用來演示如何爲UIAlertview對象添加子視圖的方法。

 UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"請等待"

 

                                                 message:nil

 

                                                 delegate:nil  

 

                                                 cancelButtonTitle:nil

 

                                                 otherButtonTitles:nil]; 

 

 [alert show];

 UIActivityIndicatorView*activeView =[[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

 activeView.center = CGPointMake(alert.bounds.size.width/2.0f,alert.bounds.size.height-40.0f); 

 [activeView startAnimating]; 

 [alert addSubview:activeView]; 

 [activeView release]; 

 [alert release]; 

 

 6.  UIAlertView默認情況下所有的text是居中對齊的。 那如果需要將文本向左對齊或者添加其他控件比如輸入框時該怎麼辦呢?不用擔心, iPhone SDK還是很靈活的, 有很多delegate消息供調用程序使用。 所要做的就是在

 -(void)willPresentAlertView:(UIAlertView *)alertView

 

 中按照自己的需要修改或添加即可,比如需要將消息文本左對齊,下面的代碼即可實現:

 -(void) willPresentAlertView:(UIAlertView*)alertView

 

 {

      for( UIView * view in alertView.subviews )

      {

            if( [view isKindOfClass:[UILabel class]] )

            {

             UILabel* label = (UILabel*)view;

                  label.textAlignment=UITextAlignmentLeft;

            }

 

      }

 

 }

  這段代碼很簡單,就是在消息框即將彈出時,遍歷所有消息框對象,將其文本對齊屬性修改爲UITextAlignmentLeft即可。

 

 添加其他部件也如出一轍,如下代碼添加兩個UITextField:

 

 -(void) willPresentAlertView:(UIAlertView *)alertView

 

 {

      CGRect frame = alertView.frame;

 

      frame.origin.y -= 120;

 

      frame.size.height += 80;

 

      alertView.frame = frame;

     for( UIView * viewin alertView.subviews )

 

      {

            if( ![viewisKindOfClass:[UILabelclass]] )

            {

                   CGRect btnFrame =view.frame;

                   btnFrame.origin.y += 70;

                   view.frame = btnFrame;

              } 

 }

 UITextField* accoutName = [[UITextFieldalloc]init];

 

 UITextField* accoutPassword =[[UITextFieldalloc] init];


 accoutName.frame = CGRectMake( 10,frame.origin.y + 40,frame.size.width - 20, 30 );

 

 accoutPassword.frame = CGRectMake( 10,frame.origin.y + 80,frame.size.width -20, 30 );

 accoutName.placeholder = @"請輸入賬號";

 accoutPassword.placeholder = @"請輸入密碼";

 accoutPassword.secureTextEntry = YES;

 [alertView addSubview:accoutPassword];

 

 [alertView addSubview:accoutName];

 [accoutName release];

 

 [accoutPassword release];

 

 }

  顯示將消息框固有的buttonlabel移位,不然添加的text field會將其遮蓋住。 然後添加需要的部件到相應的位置即可。

 

 對於UIActionSheet其實也是一樣的,

 -(void)willPresentActionSheet:(UIActionSheet *)actionSheet

 中做同樣的處理一樣可以得到自己想要的界面。

  

UIAlertView Delegate

- voidalertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

alertView--->這個不用多說了吧

buttonIndex---->0開始

可以通過if (buttonIndex ==1) { } 這樣的來控制點擊了某個按鈕需要做什麼操作

 

- voidalertView:(UIAlertView*)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

這個方法在動畫結束和視圖隱藏之後調用

 

- voidalertView:(UIAlertView*)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex

這個方法在動畫開始和視圖隱藏之前調用

 

- voidalertViewCancel:(UIAlertView*)alertView

 在視圖將要被取消之前

 例如,用戶點擊了home

 三個函數的調用順序依次是:

 alertViewCancel----willDismissWithButtonIndex---didDismissWithButtonIndex

 

- BOOLalertViewShouldEnableFirstOtherButton(UIAlertView*)alertView

ios 5+

設置yes / no  將會設置alertView 的第一個otherButtonenable屬性

 

- 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 有兩個textfieldLoginpassword

 

 只要有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

 返回值是增加的Buttonindex

 

- (NSString*)buttonTitleAtIndex:(NSInteger)buttonIndex

 輸入buttonIndex 返回button的標題

 

- void)dismissWithClickedButtonIndex:(NSInteger)buttonIndexanimated:(BOOL)animated

 程序自動完成點擊buttonIndexbutton dismiss 整個alertView的操作

 

- (id)initWithTitle:(NSString *)titlemessage:(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

 UIAlertViewStyleLoginAndPasswordInputtextfieldIndex有兩個 0 1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章