iOS使用UIAlertController三種顯示提示框代碼

UIAlertView在IOS 8以上版本已經(jīng)過(guò)時(shí)了,官方推薦我們使用UIAlertController代替UIAlertView、UIActionSheet

1?UIAlertController顯示普通的Alert

- (IBAction)showAlert:(UIButton *)sender {

//顯示提示框

//過(guò)時(shí)

// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; // [alert show];

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"This is an alert." preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {//響應(yīng)事件

NSLog(@"action = %@", action);

}];

UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { //響應(yīng)事件

NSLog(@"action = %@", action);

}];

[alert addAction:defaultAction];

[alert addAction:cancelAction];

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

}

2?UIAlertController顯示帶文本輸入的的Alert


- (IBAction)showList:(UIButton *)sender {

//提示框添加文本輸入框

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"This is an alert." preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {//響應(yīng)事件//得到文本信息

for(UITextField *text in alert.textFields){

NSLog(@"text = %@", text.text);

}??

}];

UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { //響應(yīng)事件

NSLog(@"action = %@", alert.textFields);

}];

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"登錄"; }];

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"密碼"; textField.secureTextEntry = YES; }];

[alert addAction:okAction]; [alert addAction:cancelAction];

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

}

3?UIAlertController顯示ActionSheet


- (IBAction)showSheet:(UIButton *)sender {

//顯示彈出框列表選擇

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"This is an Sheet." preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { //響應(yīng)事件 NSLog(@"action = %@", action);?

}];

UIAlertAction* deleteAction = [UIAlertAction actionWithTitle:@"刪除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) { //響應(yīng)事件 NSLog(@"action = %@", action);?

}];

UIAlertAction* saveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { //響應(yīng)事件 NSLog(@"action = %@", action);

}];

[alert addAction:saveAction]; [alert addAction:cancelAction]; [alert addAction:deleteAction]; [self presentViewController:alert animated:YES completion:nil];?

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末领猾,一起剝皮案震驚了整個(gè)濱河市破停,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌斋陪,老刑警劉巖朽褪,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異无虚,居然都是意外死亡缔赠,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門友题,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)嗤堰,“玉大人,你說(shuō)我怎么就攤上這事度宦√呦唬” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵戈抄,是天一觀的道長(zhǎng)离唬。 經(jīng)常有香客問(wèn)我,道長(zhǎng)划鸽,這世上最難降的妖魔是什么输莺? 我笑而不...
    開封第一講書人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任戚哎,我火速辦了婚禮,結(jié)果婚禮上嫂用,老公的妹妹穿的比我還像新娘型凳。我一直安慰自己,他們只是感情好嘱函,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開白布甘畅。 她就那樣靜靜地躺著,像睡著了一般往弓。 火紅的嫁衣襯著肌膚如雪疏唾。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,718評(píng)論 1 305
  • 那天亮航,我揣著相機(jī)與錄音荸实,去河邊找鬼。 笑死缴淋,一個(gè)胖子當(dāng)著我的面吹牛准给,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播重抖,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼露氮,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了钟沛?” 一聲冷哼從身側(cè)響起畔规,我...
    開封第一講書人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎恨统,沒(méi)想到半個(gè)月后叁扫,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡畜埋,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年莫绣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片悠鞍。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡对室,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出咖祭,到底是詐尸還是另有隱情掩宜,我是刑警寧澤,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布么翰,位于F島的核電站牺汤,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏浩嫌。R本人自食惡果不足惜慧瘤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一戴已、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧锅减,春花似錦、人聲如沸伐坏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)桦沉。三九已至每瞒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間纯露,已是汗流浹背剿骨。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留埠褪,地道東北人浓利。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像钞速,于是被迫代替她去往敵國(guó)和親贷掖。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

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

  • 轉(zhuǎn)載 http://www.cocoachina.com/ios/20141126/10320.html如果侵權(quán)渴语,...
    小劉_假裝是個(gè)程序員閱讀 647評(píng)論 0 0
  • 當(dāng)你的應(yīng)用程序需要向用戶呈現(xiàn)重要信息苹威,或提示用戶重要選擇時(shí),可以使用警告框(Alert View)和操作表(Act...
    Han_105a閱讀 3,191評(píng)論 0 1
  • 添加了新的類 UIAlertController 和 UIAlertAction 來(lái)取代曾經(jīng)的 UIAlertVi...
    默默的前行閱讀 3,336評(píng)論 0 2
  • 自從iOS9出來(lái)之后驾凶,需要使用UIAlertController來(lái)彈出彈框牙甫,不在提倡使用UIAlertView了,...
    南京楊小兵閱讀 562評(píng)論 1 0
  • 針對(duì)這幾個(gè)問(wèn)題调违,我想做一些疏導(dǎo)窟哺。 1.從小最需要,最沒(méi)有得到滿足的心理需求是:父母的關(guān)注和關(guān)愛(ài)翰萨。缺乏安全感脏答。 做的...
    水光瀲滟山空蒙閱讀 386評(píng)論 0 0