一唧躲、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
岳掐,它集合了alert
和actionSheet
。
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;
}
}