<span style="font-size:24px;">UIAlertView 和 UIActionSheet
<span style="font-size:12px;">
</span></span><span style="font-size:12px;">- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION
NS_EXTENSION_UNAVAILABLE_IOS("Use UIAlertController instead.");
- (instancetype)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION NS_EXTENSION_UNAVAILABLE_IOS
("Use UIAlertController instead.");</span>
我当前使用的xcode版本是6.3, 系统已经提示我们当使用UIAlertView或者UIActionSheet时 建议用UIAlertController 代替.
所以,UIAlertView和UIActionSheet 将会在iOS9中被舍弃.那么现在就看看UIAlertController的一些基本用法.
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
可以看到在iOS8.0后已经出现了UIAlertController, 它继承于UIViewController
UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两货的功能和作用。是使用对话框(alert)还是使用上拉菜单(action sheet),就取决于在创建控制器时,您是如何设置首选样式的。
它的初始化方法
+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message
preferredStyle:(UIAlertControllerStyle)preferredStyle;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
message:@"登录" preferredStyle:(UIAlertControllerStyleAlert)];
这里它的样式有两种:一种是alertView,另一种是actionSheet, 效果和以前一样
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);
不同的是,它的初始化方法中 不在有按钮了.
因为有单独的方法来添加按钮:
按钮的类型:
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0, // 常规
UIAlertActionStyleCancel, // 取消
UIAlertActionStyleDestructive // 警示
} NS_ENUM_AVAILABLE_IOS(8_0);
// handler处理点击事件, 取消类型的按钮只能创建一次, 根据苹果官方的定义,“警示”样式的按钮是用在可能会改变或删除数据的操作上。因此用这种样式创建的按钮会是红色的,来警示用户。
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定"
style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction *action) {
NSArray *arr = alert.textFields;
UITextField *tex = arr[0];
NSLog(@"%@", tex.text);
}];
[alert addAction:alertAction];
添加的按钮自带一个block回调函数.
添加textField的方法
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"呵呵呵呵";
}];
UIActionSheet用法与以上基本相似,就不叙述了!