iOS8.0 之后UIAlertView和UIActionSheet的创建方式的新变化

来源:互联网 发布:mysql分页加排序语句 编辑:程序博客网 时间:2024/05/19 22:52

最近已经换了Xcode 6.1,开始适配iOS 8.0以上版本了,在iOS 8以前,我们创建一个警告视图(UIAlertView),通常是这样的

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];    [alertView show];

运行后,如图所示:



通过UIAlertViewDelegate 中的 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
来响应按钮事件

创建一个选择按钮项( UIActionSheet),通常是这样的:
UIActionSheet *actioSheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Destructive" otherButtonTitles:@"otherButton", nil];    [actioSheet showInView:self.view];

运行后如图所示:

通过UIActionSheetDelegate中的
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
响应按钮事件

但是在Xcode 6.1 iOS 8.0 的环境下,创建这两个控件会出现这样的提示

( 重要:UIAlerView 在iOS8中已弃用,同时UIAlertViewDeleagte也过时。在iOS8以后用UIAlertController的preferredStyle类型中的UIAlertControllerStyleAlert来创建和管理警告)

虽然苹果申明UIAlertView已经被被弃用,但是我们创建UIAlertView在iOS 8下运行并没有给出任何警告……

那我们就一起来看一下这个UIAlertController 是怎么用的,下面就是UIAlertController.h的全部内容
#import <UIKit/UIViewController.h>typedef NS_ENUM(NSInteger, UIAlertActionStyle) {    UIAlertActionStyleDefault = 0,    UIAlertActionStyleCancel,    UIAlertActionStyleDestructive} NS_ENUM_AVAILABLE_IOS(8_0);typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {    UIAlertControllerStyleActionSheet = 0,    UIAlertControllerStyleAlert} NS_ENUM_AVAILABLE_IOS(8_0);NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertAction : NSObject <NSCopying>+ (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(UIAlertAction *action))handler;@property (nonatomic, readonly) NSString *title;@property (nonatomic, readonly) UIAlertActionStyle style;@property (nonatomic, getter=isEnabled) BOOL enabled;@endNS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;- (void)addAction:(UIAlertAction *)action;@property (nonatomic, readonly) NSArray *actions;- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;@property (nonatomic, readonly) NSArray *textFields;@property (nonatomic, copy) NSString *title;@property (nonatomic, copy) NSString *message;@property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;@end


使用 + 方法来创建一个UIAlertController
+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
UIAlertControllerStyle枚举内容:
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {    UIAlertControllerStyleActionSheet = 0,    UIAlertControllerStyleAlert}

显然可以通过这个方法同时来创建一个Alert 或者ActionSheet

没有了按钮标题参数,取而代之的是一个UIAction类来管理按钮选项,同样通过一个+方法来创建
+ (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(UIAlertAction *action))handler;
通过一个Block回调来处理按钮响应事件,这果然很棒!!
所以呢,在iOS 8后,创建一个AlertView,已经变成这样了(官方源代码):
NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);    NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);    NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);    NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];        // Create the actions.    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {        NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");    }];        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {        NSLog(@"The \"Okay/Cancel\" alert's other action occured.");    }];        // Add the actions.    [alertController addAction:cancelAction];    [alertController addAction:otherAction];        [self presentViewController:alertController animated:YES completion:nil];
运行后如图所示:


创建一个ActionSheet方法类似:
NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);    NSString *destructiveButtonTitle = NSLocalizedString(@"OK", nil);        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];        // Create the actions.    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {        NSLog(@"The \"Okay/Cancel\" alert action sheet's cancel action occured.");    }];        UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {        NSLog(@"The \"Okay/Cancel\" alert action sheet's destructive action occured.");    }];        // Add the actions.    [alertController addAction:cancelAction];    [alertController addAction:destructiveAction];[self presentViewController:alertController animated:YES completion:nil];
运行后如图所示:



0 0
原创粉丝点击