iOS8之后的UIAlertController

  • iOS 8之后蘋果推薦我們使用的系統自帶的提示框是UIAlertController。UIAlertController有兩種類型:
    typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
    UIAlertControllerStyleActionSheet = 0,
    UIAlertControllerStyleAlert
    } NS_ENUM_AVAILABLE_IOS(8_0);
    它的出現是為了替帶之前的UIAlertView和UIActionSheet兩個類。
  • 下面介紹一下UIAlertController,順便有示例。


    示例
  • 1.登錄型的提示框
    - (IBAction)loginAlert:(UIButton *)sender {
    //獲取用戶當前手機的系統版本
    [[UIDevice currentDevice] systemVersion];
    //當前的系統版本大于等于8.0
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"登錄" message:nil preferredStyle:UIAlertControllerStyleAlert];
    //1.初始化action
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //找到輸入框對象
    UITextField *usernameTF = alertController.textFields[0];
    UITextField *passwordTF = alertController.textFields[1];
    //取出用戶名和密碼
    NSString *username = usernameTF.text;
    NSString *password = passwordTF.text;
    NSLog(@"username:%@\npassword:%@",username,password);
    }];
    //2.添加動作
    [alertController addAction:action1];
    [alertController addAction:action2];
    //添加輸入框
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    textField.placeholder = @"請輸入用戶名";
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    textField.placeholder = @"請輸入密碼";
    textField.secureTextEntry = YES;
    }];
    //顯示
    [self presentViewController:alertController animated:YES completion:nil];
    }
    }

  • 2.UIAlertControllerStyleAlert和UIAlertControllerStyleActionSheet類型的,這里的示例是傳入一個類型,然后根據類型展示不同風格。
    - (void)btnTouchWithStyle:(UIAlertControllerStyle)style{
    /*
    preferredStyle:
    1.UIAlertControllerStyleActionSheet
    2.UIAlertControllerStyleAlert
    */
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
    //iOS8以后才有的,替換了UIAlertView和UIActionSheet兩個類
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"分享" message:@"選擇分享的平臺" preferredStyle:style];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"取消按鈕點擊");
    }];
    UIAlertAction *qqAction = [UIAlertAction actionWithTitle:@"分享到QQ" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"分享到QQ");
    }];
    UIAlertAction *weixinAction = [UIAlertAction actionWithTitle:@"分享到微信" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"分享到微信");
    }];

          //添加動作
          [alertController addAction:cancelAction];
          [alertController addAction:qqAction];
          [alertController addAction:weixinAction];
          //顯示
          [self presentViewController:alertController animated:YES completion:nil];
      }else{
          NSLog(@"當前的系統版本是小于8.0的,使用UIAlertView");
          UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享" message:@"選擇分享的平臺" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
          [alertView show];
      }
      NSLog(@"------當前的系統版本: %f---",[[[UIDevice currentDevice] systemVersion] floatValue]);
    }
    
示例代碼https://github.com/SPIREJ/SJUIAlertController
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容