iOS 8 开始使用 UIAlertController
iOS9 开始弃用 UIAlertView 和 UIActionSheet
这里简单整理一下上面三个。为了使用方便,可以自己写个类区分使用。
UIAlertController
新东西就是方便好用,代码量减少,都不用代理了,用起来还更简单了
简单过程:新建 UIAlertController 对其添加 UIAlertAction ,然后 对ViewController 进行 present/dismiss 就可以了。
- 新版 普通 Alert
// 初始化 添加 提示内容
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"messgae" preferredStyle:UIAlertControllerStyleAlert];
/*
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,// 不能加输入框,其他一样
UIAlertControllerStyleAlert // 可以添加输入框
} 弹出类型;
*/
// 添加 AlertAction 事件回调(三种类型:默认,取消,警告)
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"ok");
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"cancel");
}];
UIAlertAction *errorAction = [UIAlertAction actionWithTitle:@"error" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"error");
}];
// cancel类自动变成最后一个,警告类推荐放上面
[alertController addAction:errorAction];
[alertController addAction:okAction];
[alertController addAction:cancelAction];
// 出现
[self presentViewController:alertController animated:YES completion:^{
NSLog(@"presented");
}];
// 移除
[alertController dismissViewControllerAnimated:YES completion:^{
NSLog(@"dismiss");
}];
- 新版 带输入的 Alert
// AlertController 直接添加 textField 即可!
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"name";
}];
// 添加 action,再其回调中可以处理输入内容
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 获取上面的输入框
UITextField *tempField = [alertController.textFields firstObject];
NSLog(@"%@",tempField.text);
NSLog(@"ok");
}];
- 新版 ActionSheet
// 提示内容 初始化 AlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"messgae" preferredStyle:UIAlertControllerStyleActionSheet];
//其余的完全一致,只是添加 TextField 会报错而已。
旧版 UIAlertView
// 初始化 并 show,other可以多个
self.alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"messgae" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", @"error", nil];
self.alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[self.alert show];
/*
typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
UIAlertViewStyleDefault = 0,// 提示
UIAlertViewStyleSecureTextInput,// 密码输入
UIAlertViewStylePlainTextInput,// 一般输入
UIAlertViewStyleLoginAndPasswordInput// 2行,姓名密码
}; AlertView 类型
*/
// 代码直接dismiss,点击btn不需要调用
[self.alert dismissWithClickedButtonIndex:0 animated:YES];
/* 获取信息
- (nullable NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
@property(nonatomic,readonly) NSInteger numberOfButtons;
@property(nonatomic) NSInteger cancelButtonIndex;
@property(nonatomic,readonly) NSInteger firstOtherButtonIndex;
@property(nonatomic,readonly,getter=isVisible) BOOL visible;
- (nullable UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex NS_AVAILABLE_IOS(5_0);
*/
}
#pragma mark - delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"%zi",buttonIndex);
// 对应button 处理(buttonIndex cancel永远是0,然后从上往下增加)
}
// 下面的代理都不常用。
- (void)alertViewCancel:(UIAlertView *)alertView {
NSLog(@"cancel?");
}
- (void)willPresentAlertView:(UIAlertView *)alertView {
NSLog(@"will");
}
- (void)didPresentAlertView:(UIAlertView *)alertView {
NSLog(@"did");
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@"will - dismiss");
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@"did - dismiss");
}
// 处理button 是否可用,cancel 不被设置
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
// 可以获取输入框内容来判断 button 是否可用。
UITextField *nameField = [self.alert textFieldAtIndex:0];
UITextField *passwordField = [self.alert textFieldAtIndex:1];
if (nameField.text.length > 2 && passwordField.text.length > 2) {
return YES;
}
return NO;
}
旧版 UIActionSheet
相比alertView 很简单,注意cancel 的 获取不一样!
// 初始化 并show
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"error" otherButtonTitles:@"ok", @"ok2", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
/*
typedef NS_ENUM(NSInteger, UIActionSheetStyle) {
UIActionSheetStyleAutomatic = -1, // take appearance from toolbar style otherwise uses 'default'
UIActionSheetStyleDefault = UIBarStyleDefault,
UIActionSheetStyleBlackTranslucent = UIBarStyleBlackTranslucent,
UIActionSheetStyleBlackOpaque = UIBarStyleBlackOpaque ,
} showInView 就是Default,设置无效? 有什么软用?;
*/
// 额外还有几个 show,感觉没什么软用
[actionSheet showInView:self.view];
// [actionSheet showFromBarButtonItem:self.leftItem animated:YES];
// 代码强行dismiss(点击按钮不需要用)
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
// 获取 信息
/*
@property(nonatomic,copy) NSString *title;
- (NSInteger)addButtonWithTitle:(nullable NSString *)title;
- (nullable NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
@property(nonatomic,readonly) NSInteger numberOfButtons;
@property(nonatomic) NSInteger cancelButtonIndex;
@property(nonatomic) NSInteger destructiveButtonIndex;
@property(nonatomic,readonly) NSInteger firstOtherButtonIndex;
@property(nonatomic,readonly,getter=isVisible) BOOL visible;
*/
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"%zi",buttonIndex);
// button 点击处理(然后从上往下增加从0开始,注意与alertView 不一样!)
}
// 下面的基本不怎么用
- (void)actionSheetCancel:(UIActionSheet *)actionSheet {
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
}
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet {
}
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
}
1