UIAlertView和UIActionSheet的IOS8兼容

来源:互联网 发布:sql语句求和1到50 编辑:程序博客网 时间:2024/06/05 17:05

IOS8中,UIActiconSheet已被废弃,同时基于UIActionSheet自定义的也将无效果。

Apple将UIActionSheet和UIAlertView整合成一个接口UIAlertController。

原来的是一个view,展示在window视图之上。现在改成了controller,展示方式变成由当前的controller直接出来。

在项目中添加以下文件,可以在兼容

UIActionSheet 兼容

.h文件

#import <UIKit/UIKit.h>#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)@interface UIActionSheet (simplyShowInViewAddition)- (void) simplyShowInView:(UIView *) view ;@end

.m文件

#import "UIActionSheet+simplyShowInViewAddition.h"@implementation UIActionSheet (simplyShowInViewAddition)- (void) simplyShowInView:(UIView *) view{    if(SYSTEM_VERSION_LESS_THAN(@"8.0"))    {        [self showInView:view];    }    else    {        // "Translating" UIActionSheet to UIAlertController for better compatibility with iOS 8        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:self.title preferredStyle:UIAlertControllerStyleActionSheet];        int nactions = [self numberOfButtons];        int i=0;        while(i<nactions)        {            NSString *button_title=[self buttonTitleAtIndex:i];            UIAlertActionStyle style=UIAlertActionStyleDefault;            if(i==[self cancelButtonIndex])            {                style = UIAlertActionStyleCancel;            }            else if(i==[self destructiveButtonIndex])            {                style = UIAlertActionStyleDestructive;            }                        UIAlertAction *newAction = [UIAlertAction actionWithTitle:button_title style:style handler:^(UIAlertAction *action) {                NSLog(@"clicked action %d",i);                [self.delegate actionSheet:self clickedButtonAtIndex:i];            }];                        [alert addAction:newAction];            i++;        }                if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)        {            [alert setModalPresentationStyle:UIModalPresentationPopover];                        UIPopoverPresentationController *popPresenter = [alert                                                             popoverPresentationController];            popPresenter.sourceView = view;            popPresenter.sourceRect = CGRectMake(view.frame.size.width/2-1, 0.45*view.frame.size.height, 2, 1);            popPresenter.permittedArrowDirections = 0;        }        UIViewController *sourceViewController;        if(self.delegate!=nil && [self.delegate respondsToSelector:@selector(presentViewController:animated:completion:)])        {            NSLog(@"presenting UIAlertController on source view controller");            sourceViewController=(UIViewController *)(self.delegate);        }        else        {            // When the actionsheet delegate is not a UIViewController            NSLog(@"presenting UIAlertController on displayed view controller");#warning Set up a method here to obtain the view controller where you want to display it            sourceViewController = /* Set up a method here to obtain the view controller where you want to display it */        }        [sourceViewController presentViewController:alert animated:YES completion:nil];    }}

UIAlertView 只要对代码稍加修改就行了,就不写出了!



0 0