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

来源:互联网 发布:网络高利贷曝光吧 编辑:程序博客网 时间:2024/05/17 12:56
原链http://my.oschina.net/u/1418722/blog/318759
IOS8 后,AlertView和ActionSheet被合并了,替代者是AlertController。将原来的delegate方式更换为block方法。这样,需要多版本适配的项目就悲剧了。我封装了一个通用类,用于解决这个问题。
IOS8 兼容性 适配 AlertView ActionSheet

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

AlertView的情况

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
- (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的情况

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
- (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


封装类代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//  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
0 0
原创粉丝点击