文档章节

IOS8 AlertView和ActionSheet不兼容问题解决方案

 泊竹
发布于 2014/09/26 10:00
字数 718
阅读 6109
收藏 2

具体代码附文末。先演示一下怎么使用。

AlertView的情况

- (void)creatAlertView
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
}

#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            
            break;
        case 1:
            
            break;
            
        default:
            break;
    }
}

#pragma mark Use NewObject(AlertView)
- (void)creatAlertView
{
    BOAlertController *alertView = [[BOAlertController alloc] initWithTitle:@"title" message:@"message" viewController:self];
    
    RIButtonItem *cancelItem = [RIButtonItem itemWithLabel:@"取消" action:^{
        
    }];
    [alertView addButton:cancelItem type:RIButtonItemType_Cancel];
    
    RIButtonItem *okItem = [RIButtonItem itemWithLabel:@"确定" action:^{
        
    }];
    [alertView addButton:okItem type:RIButtonItemType_Destructive];
    [alertView show];
}

ActionSheet的情况

- (void)creatActionSheet
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其它功能", nil];
    [actionSheet showInView:self.view];
}

#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            
            break;
        case 1:
            
            break;
            
        default:
            break;
    }
}

#pragma mark Use NewObject(ActionSheet)
- (void)creatActionSheet
{
    BOAlertController *actionSheet = [[BOAlertController alloc] initWithTitle:@"title" message:nil viewController:self];
    
    RIButtonItem *cancelItem = [RIButtonItem itemWithLabel:@"取消" action:^{
        
    }];
    [actionSheet addButton:cancelItem type:RIButtonItemType_Cancel];
    
    RIButtonItem *okItem = [RIButtonItem itemWithLabel:@"确定" action:^{
        
    }];
    [actionSheet addButton:okItem type:RIButtonItemType_Destructive];
    
    [actionSheet showInView:self.view];
}


使用这个封装类需要扩展类UIAlertView-Blocks的支持。下载地址https://github.com/jivadevoe/UIAlertView-Blocks


封装类代码

//  Created by 孙俊 on 14-9-25.
//  Copyright (c) 2014年 yipinapp.ibrand. All rights reserved.
//

typedef enum {
    RIButtonItemType_Cancel         = 1,
    RIButtonItemType_Destructive       ,
    RIButtonItemType_Other
}RIButtonItemType;

typedef enum {
    BOAlertControllerType_AlertView    = 1,
    BOAlertControllerType_ActionSheet
}BOAlertControllerType;

#define isIOS8  ([[[UIDevice currentDevice]systemVersion]floatValue]>=8)

#import <Foundation/Foundation.h>
#import "RIButtonItem.h"

@interface BOAlertController : NSObject

- (id)initWithTitle:(NSString *)title message:(NSString *)message viewController:(UIViewController *)inViewController;
- (void)addButton:(RIButtonItem *)button type:(RIButtonItemType)itemType;

//Show ActionSheet in all versions
- (void)showInView:(UIView *)view;

//Show AlertView in all versions
- (void)show;

@end

#import "BOAlertController.h"
#import "UIActionSheet+Blocks.h"
#import "UIAlertView+Blocks.h"

@interface BOAlertController()

@property (nonatomic, strong) NSString              *title;
@property (nonatomic, strong) NSString              *message;
@property (nonatomic, weak) UIViewController        *inViewController;
@property (nonatomic, strong) NSMutableDictionary   *buttonInfoDict;

@end

@implementation BOAlertController

- (id)initWithTitle:(NSString *)title message:(NSString *)message viewController:(UIViewController *)inViewController
{
    if (self = [super init]) {
        self.title = title;
        self.message = message;
        self.inViewController = inViewController;
    }
    return self;
}

- (NSMutableDictionary *)buttonInfoDict
{
    if (_buttonInfoDict == nil) {
        _buttonInfoDict = [NSMutableDictionary dictionary];
    }
    return _buttonInfoDict;
}

- (void)addButton:(RIButtonItem *)button type:(RIButtonItemType)itemType
{
    if (button == nil || ![button isKindOfClass:[RIButtonItem class]]) {
        return;
    }
    switch (itemType) {
        case RIButtonItemType_Cancel:
        {
            [self.buttonInfoDict setObject:button forKey:@"RIButtonItemType_Cancel"];
        }
            break;
        case RIButtonItemType_Destructive:
        {
            [self.buttonInfoDict setObject:button forKey:@"RIButtonItemType_Destructive"];
        }
            break;
        case RIButtonItemType_Other:
        {
            NSMutableArray *otherArray = self.buttonInfoDict[@"RIButtonItemType_Other"];
            if (otherArray == nil) {
                otherArray = [NSMutableArray array];
            }
            [otherArray addObject:button];
            [self.buttonInfoDict setObject:otherArray forKey:@"RIButtonItemType_Other"];
        }
            break;
            
        default:
            break;
    }
}

- (void)showInView:(UIView *)view
{
    if (isIOS8) {
        [self showIOS8ViewWithType:BOAlertControllerType_ActionSheet];
        
    } else {
        [self ios7showInView:view];
    }
}

- (void)show
{
    if (isIOS8) {
        [self showIOS8ViewWithType:BOAlertControllerType_AlertView];
        
    } else {
        [self ios7Show];
    }
}

- (void)showIOS8ViewWithType:(BOAlertControllerType)viewType
{
    if (self.inViewController == nil) {
        return;
    }
    
    UIAlertController *alertController = nil;
    switch (viewType) {
        case BOAlertControllerType_AlertView:
        {
            alertController = [UIAlertController alertControllerWithTitle:self.title message:self.message preferredStyle:UIAlertControllerStyleAlert];
        }
            break;
        case BOAlertControllerType_ActionSheet:
        {
            alertController = [UIAlertController alertControllerWithTitle:self.title message:self.message preferredStyle:UIAlertControllerStyleActionSheet];
        }
            break;
            
        default:
            break;
    }
    
    RIButtonItem *cancelItem = self.buttonInfoDict[@"RIButtonItemType_Cancel"];
    if (cancelItem != nil) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelItem.label style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            cancelItem.action();
        }];
        [alertController addAction:cancelAction];
    }
    
    RIButtonItem *destructiveItem = self.buttonInfoDict[@"RIButtonItemType_Destructive"];
    if (destructiveItem != nil) {
        UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveItem.label style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            destructiveItem.action();
        }];
        [alertController addAction:destructiveAction];
    }
    
    NSArray *otherItems = self.buttonInfoDict[@"RIButtonItemType_Other"];
    if (otherItems != nil && otherItems.count > 0) {
        for (RIButtonItem *buttonItem in otherItems) {
            UIAlertAction *otherAction = [UIAlertAction actionWithTitle:buttonItem.label style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                buttonItem.action();
            }];
            [alertController addAction:otherAction];
        }
    }
    
    [self.inViewController presentViewController:alertController animated:YES completion:nil];
}

- (void)ios7showInView:(UIView *)view
{
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:self.title cancelButtonItem:self.buttonInfoDict[@"RIButtonItemType_Cancel"] destructiveButtonItem:self.buttonInfoDict[@"RIButtonItemType_Destructive"] otherButtonItems: nil];
    
    NSArray *otherItems = self.buttonInfoDict[@"RIButtonItemType_Other"];
    if (otherItems != nil && otherItems.count > 0) {
        for (RIButtonItem *buttonItem in otherItems) {
            [actionSheet addButtonItem:buttonItem];
        }
    }
    [actionSheet showInView:view];
}

- (void)ios7Show
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:self.title message:self.message cancelButtonItem:self.buttonInfoDict[@"RIButtonItemType_Cancel"] otherButtonItems:nil];
    
    RIButtonItem *destructiveItem = self.buttonInfoDict[@"RIButtonItemType_Destructive"];
    if (destructiveItem != nil) {
        [alertView addButtonItem:destructiveItem];
    }
    
    NSArray *otherItems = self.buttonInfoDict[@"RIButtonItemType_Other"];
    if (otherItems != nil && otherItems.count > 0) {
        for (RIButtonItem *buttonItem in otherItems) {
            [alertView addButtonItem:buttonItem];
        }
    }
    
    [alertView show];
}

@end


© 著作权归作者所有

共有 人打赏支持
粉丝 90
博文 67
码字总数 13285
作品 0
徐汇
私信 提问
UIActionSheet&UIAlertView

UIActionSheet .h 文件 @interface ViewController : UIViewController<UIActionSheetDelegate> - (IBAction)ShowActionSheet:(id)sender; .m 文件 - (IBAction)ShowActionSheet:(id)sender......

微笑de辛翼
2013/01/10
0
1
UIActionSheet的使用

UIActionSheet是在IOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件。 为了快速完成这例子,我们打开Xcode 4.3.2, 先建立一个single view application。然后再xib文件添加一个butto...

jackyyang
2012/07/14
0
0
Vant Weapp 0.3.0 发布,有赞小程序 UI 组件库

Vant Weapp 是有赞移动端组件库 Vant 的小程序版本,两者基于相同的视觉规范,提供一致的 API 接口,助力开发者快速搭建小程序应用。 Vant Weapp 0.3.0 更新日志: Breaking changes Actions...

王练
09/08
0
0
UIProgressView

做了一个小Deme,是通过UIActionSheet来显示UIProgressView进度条的。 代码如下: @interface View21 : UIViewController <UIActionSheetDelegate> { float amountDone; UIProgressView *pro......

JohnZr
2012/08/24
0
0
UIActionSheet UISlider 的简单使用

// Created by 妖精的尾巴 on 15-8-17. // Copyright (c) 2015年 妖精的尾巴. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UIAlertViewDelegate,UIA......

不孤独的美食家
2015/08/17
0
0

没有更多内容

加载失败,请刷新页面

加载更多

jena RDF学习笔记

一、首先要了解RDF和Jena RDF的介绍: An Introduction to RDF and the Jena RDF API RDF及Jena RDF API介绍(中翻) 在Jena中,用于表示单个三元组的Java类是Statement。根据RDF规范,只有资...

孙幼凌
27分钟前
1
0
HTML5 240行代码实现粒子漩涡特效

效果 源码如下 <!doctype html><html><head><meta charset="utf-8"><title>H5,200行代码实现粒子漩涡特效</title><style>html,body{margin:0px;width:100%;height:100%......

lanyu96
33分钟前
1
0
fine report

FineReport xx no: 0941626c-5e513d196-f528-74a7c52d8e61

youngjdong
34分钟前
2
0
get right firefox version for your selenium

find the selenium location path find / -name selenium output: /usr/local/lib/python2.7/dist-packages/selenium get the "webdriver.xpi" file, the file is under "/usr/local/lib/pyt......

关上越
42分钟前
1
0
为小程序端添加一个session机制

我们做后台用户认证的时候一般会借助于session机制,非常方便,今天我们使用小程序的Storage机制自己实现一个session。 本文思路来自于前几日对微信wafer的源码阅读,感谢wafer所有代码编写者...

阿北2017
45分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部