iOS8之后的UIAlertController

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


    示例
  • 1.登錄型的提示框
    - (IBAction)loginAlert:(UIButton *)sender {
    //獲取用戶當(dāng)前手機(jī)的系統(tǒng)版本
    [[UIDevice currentDevice] systemVersion];
    //當(dāng)前的系統(tǒng)版本大于等于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) {
    //找到輸入框?qū)ο?br> UITextField *usernameTF = alertController.textFields[0];
    UITextField *passwordTF = alertController.textFields[1];
    //取出用戶名和密碼
    NSString *username = usernameTF.text;
    NSString *password = passwordTF.text;
    NSLog(@"username:%@\npassword:%@",username,password);
    }];
    //2.添加動(dòng)作
    [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類型的,這里的示例是傳入一個(gè)類型,然后根據(jù)類型展示不同風(fēng)格山憨。
    - (void)btnTouchWithStyle:(UIAlertControllerStyle)style{
    /*
    preferredStyle:
    1.UIAlertControllerStyleActionSheet
    2.UIAlertControllerStyleAlert
    */
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
    //iOS8以后才有的,替換了UIAlertView和UIActionSheet兩個(gè)類
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"分享" message:@"選擇分享的平臺(tái)" preferredStyle:style];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"取消按鈕點(diǎn)擊");
    }];
    UIAlertAction *qqAction = [UIAlertAction actionWithTitle:@"分享到QQ" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"分享到QQ");
    }];
    UIAlertAction *weixinAction = [UIAlertAction actionWithTitle:@"分享到微信" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"分享到微信");
    }];

          //添加動(dòng)作
          [alertController addAction:cancelAction];
          [alertController addAction:qqAction];
          [alertController addAction:weixinAction];
          //顯示
          [self presentViewController:alertController animated:YES completion:nil];
      }else{
          NSLog(@"當(dāng)前的系統(tǒng)版本是小于8.0的,使用UIAlertView");
          UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享" message:@"選擇分享的平臺(tái)" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
          [alertView show];
      }
      NSLog(@"------當(dāng)前的系統(tǒng)版本: %f---",[[[UIDevice currentDevice] systemVersion] floatValue]);
    }
    
示例代碼https://github.com/SPIREJ/SJUIAlertController
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末冕末,一起剝皮案震驚了整個(gè)濱河市萍歉,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌档桃,老刑警劉巖枪孩,帶你破解...
    沈念sama閱讀 216,470評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異藻肄,居然都是意外死亡蔑舞,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門嘹屯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來攻询,“玉大人,你說我怎么就攤上這事州弟【埽” “怎么了低零?”我有些...
    開封第一講書人閱讀 162,577評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長拯杠。 經(jīng)常有香客問我掏婶,道長,這世上最難降的妖魔是什么潭陪? 我笑而不...
    開封第一講書人閱讀 58,176評(píng)論 1 292
  • 正文 為了忘掉前任雄妥,我火速辦了婚禮,結(jié)果婚禮上依溯,老公的妹妹穿的比我還像新娘老厌。我一直安慰自己,他們只是感情好黎炉,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評(píng)論 6 388
  • 文/花漫 我一把揭開白布枝秤。 她就那樣靜靜地躺著,像睡著了一般拜隧。 火紅的嫁衣襯著肌膚如雪宿百。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,155評(píng)論 1 299
  • 那天洪添,我揣著相機(jī)與錄音垦页,去河邊找鬼。 笑死干奢,一個(gè)胖子當(dāng)著我的面吹牛痊焊,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播忿峻,決...
    沈念sama閱讀 40,041評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼薄啥,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了逛尚?” 一聲冷哼從身側(cè)響起垄惧,我...
    開封第一講書人閱讀 38,903評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎绰寞,沒想到半個(gè)月后到逊,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,319評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡滤钱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評(píng)論 2 332
  • 正文 我和宋清朗相戀三年觉壶,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片件缸。...
    茶點(diǎn)故事閱讀 39,703評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡铜靶,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出他炊,到底是詐尸還是另有隱情争剿,我是刑警寧澤已艰,帶...
    沈念sama閱讀 35,417評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站秒梅,受9級(jí)特大地震影響旗芬,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜捆蜀,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評(píng)論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望幔嫂。 院中可真熱鬧辆它,春花似錦、人聲如沸履恩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽切心。三九已至飒筑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間绽昏,已是汗流浹背协屡。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留全谤,地道東北人肤晓。 一個(gè)月前我還...
    沈念sama閱讀 47,711評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像认然,于是被迫代替她去往敵國和親补憾。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容