UIAlerView和UIAlertController的使用

一唧躲、UIAlerView

UIAlertView已經(jīng)過期了碱璃,9.0或以上的版本使用會提示過期提醒弄痹,但是可以正常使用。最好還是改為使用UIAlertController替代嵌器。

warning

1. 定制彈框的標(biāo)題肛真、內(nèi)容和按鈕

...
@property (nonatomic, strong) UIAlertView *alert;
...

self.alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"此UIAlertView已經(jīng)過期,應(yīng)該使用UIAlertController替代" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的",@"知道了",nil];

2. 定制彈框的樣式

有四種樣式:
UIAlertViewStyleDefault : 沒有輸出框嘴秸,只是普通的彈框
UIAlertViewStyleSecureTextInput 有一個安全密碼輸入框
UIAlertViewStylePlainTextInput 有一個普通文本輸入框
UIAlertViewStyleLoginAndPasswordInput 有兩個輸入框:文本和密碼

[self.alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[self.alert show]; // 執(zhí)行顯示彈框
showAlert

3. 彈框中的普通文本和密碼輸入框使用

// 當(dāng)前控制器遵守代理:<UIAlertViewDelegate>
...
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UITextField *passwordField;
...

self.textField = [self.alert textFieldAtIndex:0];
self.passwordField = [self.alert textFieldAtIndex:1];
self.textField.placeholder = @"用戶民";
self.passwordField.placeholder = @"密碼";

 #pragma mark - UIAlerViewDelegate
/**
 *  根據(jù)用戶按下不同按鈕執(zhí)行不同的邏輯
 *
 *  @param alertView   彈框
 *  @param buttonIndex 按鈕索引
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        NSLog(@"按下了 好的 按鈕");
        NSLog(@"用戶名:%@,密碼:%@",self.textField.text,self.passwordField.text);
    }else if (buttonIndex == 2) {
        NSLog(@"按下了 知道了 按鈕");
    }else if (buttonIndex == 3) {
        NSLog(@"按下了 贊一個 按鈕");
    }
}

/**
 *  決定第一個按鈕是否啟用
 *
 *  @param alertView 彈框
 *
 *  @return 布爾值
 */
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
    if ([[self.textField text]length] == 0 || [[self.passwordField text]length] == 0) {
        return NO;
    }
    return YES;
}

4. 其他屬性

應(yīng)用退到后臺彈框還是沒有消失毁欣,這個方法可以給你控制按鈕的選擇

[self.alert dismissWithClickedButtonIndex:2 animated:YES];
[self.alert addButtonWithTitle:@"贊一個"];
NSLog(@"按鈕的個數(shù)為:%ld",[self.alert numberOfButtons]);
NSLog(@"取消按鈕的索引為:%ld",[self.alert cancelButtonIndex]);
NSLog(@"彈框是否可見:%d",[self.alert isVisible]);
NSLog(@"第2個索引的按鈕標(biāo)題為:%@",[self.alert buttonTitleAtIndex:2]);
NSLog(@"第一個其他按鈕的索引:%ld",[self.alert firstOtherButtonIndex]);

二庇谆、UIAlertController

在iOS 8.0 版本中新增了 UIAlertController 來取代 UIAlertView岳掐,它集合了alertactionSheet

1. 定制彈框的標(biāo)題饭耳、內(nèi)容和按鈕

- (void)showAlert {
  __weak typeof(self) weakself = self;
  self.alert = [UIAlertController alertControllerWithTitle:@"UIAlertController" message:@"8.0以上使用UIAlertController替換UIAlertView" preferredStyle:UIAlertControllerStyleAlert];
    
  UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"點擊了取消按鈕");
     [[NSNotificationCenter defaultCenter]removeObserver:weakself name:UITextFieldTextDidChangeNotification object:nil];
    }];
    
  UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"點擊了好的按鈕");
    NSLog(@"用戶名:%@",[[[self.alert textFields]firstObject]text]);
    NSLog(@"密碼:%@",[[[self.alert textFields]lastObject]text]);
    [[NSNotificationCenter defaultCenter]removeObserver:weakself name:UITextFieldTextDidChangeNotification object:nil];
  }];
    
  // 先凍結(jié) “好的” 按鈕串述,需要用戶輸入用戶名和密碼后再啟用
  [defaultAction setEnabled:NO];
    
  [self.alert addAction:cancleAction];
  [self.alert addAction:defaultAction];
    
  // 添加文本輸入框
  [self.alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
      textField.placeholder = @"請輸入用戶名";
      [[NSNotificationCenter defaultCenter]addObserver:weakself selector:@selector(handleTextFieldDidChanged:) name:UITextFieldTextDidChangeNotification object:nil];
  }];
    
  // 添加密碼輸入框
  [self.alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
      textField.placeholder = @"請輸入密碼";
      textField setSecureTextEntry:YES];
  }];
    
  [self presentViewController:self.alert animated:YES completion:nil];
}

- (void)handleTextFieldDidChanged:(NSNotification *)notification {
  UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
  if (alertController) {
      UITextField *textField = alertController.textFields.firstObject;
      UIAlertAction *action  = alertController.actions.lastObject;
      action.enabled = textField.text.length > 0;
  }
}

Demo地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市寞肖,隨后出現(xiàn)的幾起案子纲酗,更是在濱河造成了極大的恐慌,老刑警劉巖新蟆,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件觅赊,死亡現(xiàn)場離奇詭異,居然都是意外死亡琼稻,警方通過查閱死者的電腦和手機(jī)吮螺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人鸠补,你說我怎么就攤上這事萝风。” “怎么了紫岩?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵规惰,是天一觀的道長。 經(jīng)常有香客問我泉蝌,道長歇万,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任勋陪,我火速辦了婚禮堕花,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘粥鞋。我一直安慰自己缘挽,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布呻粹。 她就那樣靜靜地躺著壕曼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪等浊。 梳的紋絲不亂的頭發(fā)上腮郊,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天,我揣著相機(jī)與錄音筹燕,去河邊找鬼轧飞。 笑死,一個胖子當(dāng)著我的面吹牛撒踪,可吹牛的內(nèi)容都是我干的过咬。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼制妄,長吁一口氣:“原來是場噩夢啊……” “哼掸绞!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起耕捞,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤衔掸,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后俺抽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體敞映,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年磷斧,在試婚紗的時候發(fā)現(xiàn)自己被綠了振愿。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片诗芜。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖埃疫,靈堂內(nèi)的尸體忽然破棺而出伏恐,到底是詐尸還是另有隱情,我是刑警寧澤栓霜,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布翠桦,位于F島的核電站,受9級特大地震影響胳蛮,放射性物質(zhì)發(fā)生泄漏销凑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一仅炊、第九天 我趴在偏房一處隱蔽的房頂上張望斗幼。 院中可真熱鬧,春花似錦抚垄、人聲如沸蜕窿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽桐经。三九已至,卻和暖如春浙滤,著一層夾襖步出監(jiān)牢的瞬間阴挣,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工纺腊, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留畔咧,地道東北人。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓揖膜,卻偏偏與公主長得像誓沸,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子次氨,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354

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