IOS基礎UI之(五)UIAlertView、UIActionSheet和UIAlertController詳解

     iOS 8的新特性之一就是讓接口更有適應性、更靈活,因此許多視圖控制器的實現方式發生了巨大的變化。比如說Alert Views、Action Sheets。 下面就大致介紹它們的使用方式。

    UIAlertView:

  1.創建UIAlertView。 UIAlertView的按鈕是水平排列的,當按鈕多的時候由於考慮的位置不夠,因此會垂直排列。

參數:delegate: 代理    otherButtonTitles: 其它按鈕,可以添加多個。多個用逗號隔開

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"標題" message:@"這個是UIAlertViewStyleDefault的默認樣式" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登錄",nil ];

 2.設置對話框樣式。UIAlertView樣式有四種:

UIAlertViewStyleDefault  默認樣式

UIAlertViewStylePlainTextInput  可輸入文本樣式

UIAlertViewStyleSecureTextInput可輸入密碼樣式

UIAlertViewStyleLoginAndPasswordInput 可輸入文本和密碼

alert.alertViewStyle = UIAlertViewStyleDefault;

 3.顯示對話框

[alert show];
效果:



4。 監聽點擊按鈕觸發事件。 

現在我們要需求是,用戶名和密碼不爲空,登錄按鈕纔可點擊。點擊登陸按鈕控制檯輸入相應的用戶名和密碼。如何實現呢???

實現方法:實現UIAlertViewDelegate協議,調用響應對話框視圖的按鈕動作的回調方法。還有當文本框內容改變時,調用alertViewShouldEnableOtherButton:方法可以讓按鈕動態地可用或者不可用。

  4.1 監聽對話框點擊按鈕方法實現控制檯輸出:

/**
 *  alert按鈕監聽事件
 *
 *  @param alertView   alertview
 *  @param buttonIndex 按鈕下標(從0開始)
 */
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    //NSInteger num = [alertView numberOfButtons];//對話框的按鈕個數
   // NSLog(@"對話框共%ld個按鈕",num);
    if (buttonIndex == 1) {//登錄按鈕
        NSString *name = [alertView textFieldAtIndex:0].text; //第一個輸入框的值
        NSString *password = [alertView textFieldAtIndex:1].text;//第二個輸入框的值
        NSLog(@"用戶名:%@,密碼:%@",name,password);
    }
}
 

  4.2 當文本框內容改變時,調用alertViewShouldEnableOtherButton:方法可以讓按鈕動態地可用或者不可用

/**
 *  當提示框的文本框內容改變時觸發
 *
 *  @param alertView alertView
 *
 *  @return 是否可用
 */
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView{
    BOOL isLogin = NO;
    NSString *name = [alertView textFieldAtIndex:0].text; //第一個輸入框的值
    NSString *password = [alertView textFieldAtIndex:1].text;//第二個輸入框的值
    if (name.length!=0 && password.length!=0) {
       isLogin = YES;
    }
    return isLogin;
}

                                                          


UIActionSheet

 UIActionSheet是按鈕垂直排列的上拉菜單。 ios8.3之後已經廢棄了。

 NS_CLASS_DEPRECATED_IOS(2_0,8_3,"UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead")

  UIActionSheet使用方式很簡單。

  1.創建UIActionSheet。

按鈕樣式:常規(default)、取消(cancel)以及警示(destruective), 其中警示(destruective)會顯示紅色。

UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"退出" otherButtonTitles:@"版本更新",@"反饋", nil ];
  2.顯示對話框

[sheet showInView:self.view];
                              

UIAlertController

 蘋果官方在ios8以後推薦使用UIAlertController。 UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一種模塊化替換的方式來代替UIAlertView和UIActionSheet的功能和作用。是使用對話框(alert)還是使用上拉菜單(action sheet),就取決於在創建控制器時設置首選樣式的。

  UIAlertControllerStyleActionSheet  --->   UIActionSheet

 UIAlertControllerStyleAlert   ----> UIAlertView

 

 1.創建UIAlertController。 

     注意:UIAlertController無需指定代理,也無需在初始化過程中指定按鈕

 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"標題" message:@"這是默認樣式UIAlertControllerStyleActionSheet" preferredStyle:UIAlertControllerStyleActionSheet];

2.創建UIAlertAction,將動作按鈕添加到控制器上。UIAlertAction由標題字符串、樣式以及當用戶選中該動作時運行的代碼塊組成。UIAlertActionStyle三種動作樣式:常規(default)、取消(cancel)以及警示(destruective)。

  注意:取消按鈕是唯一的,如果添加了第二個取消按鈕,程序會運行時拋出異常:

* Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘UIAlertController can only have one action with a style of UIAlertActionStyleCancel’

 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *loginout = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDestructive handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"反饋" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:cancelAction];
    [alert addAction:loginout];
    [alert addAction:okAction];

3.顯示

[self presentViewController:alert animated:YES completion:nil];

                                                          


  以上是UIAlertController實現UIActionSheet(不能實現可輸入)。下面通過UIAlertController實現UIAlertView,效果和上面UIAlertView一樣,可輸入文本和密碼,並且控制按鈕的狀態,控制檯輸入結果。

    1.創建。注意:樣式爲UIAlertControllerStyleAlert

   UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登錄窗口" message:@"請填寫登錄信息" preferredStyle:UIAlertControllerStyleAlert];

   2.創建UIAlertAction,將動作按鈕添加到控制器上

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

    UIAlertAction *login = [UIAlertAction actionWithTitle:@"登錄" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        NSString *name =  alert.textFields.firstObject.text;
        NSString *pwd = alert.textFields.lastObject.text;
        NSLog(@"用戶名:%@,密碼:%@",name,pwd);
        //點擊登陸按鈕後,銷燬觀察者對象
        [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
    }];
    login.enabled = NO;//登陸按鈕不可點擊
    [alert addAction:cancel];
    [alert addAction:login];


3.添加可輸入框並且通知觀察者(notification observer)--UITextFieldTextDidChangeNotification,判斷更改按鈕狀態

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"用戶名";
        //通知觀察者
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(alertTextFiledDidChange:) name:UITextFieldTextDidChangeNotification object:textField ];
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"密碼";
        textField.secureTextEntry = YES;
         //通知觀察者
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFiledDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
    }];

4.接受消息,更改按鈕狀態

/**
 *  觀察者消息方法,更改按鈕的狀態
 *
 *  @param notification 消息
 */
-(void)alertTextFiledDidChange:(NSNotificationCenter *)notification{
    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
    if(alertController){
        UIAlertAction *loginAction = alertController.actions.lastObject;
        NSString *name= alertController.textFields.firstObject.text;
        NSString *pwd = alertController.textFields.lastObject.text;
        loginAction.enabled = (name.length!=0 && pwd.length!=0);
    }
}

 5.顯示

 [self presentViewController:alert animated:YES completion:nil];


  

     代碼下載:代碼下載地址


      文字至此!後面學習將繼續更新。。。。。。。。。。。。

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