-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIAlertView+iOS8Ext.m
67 lines (56 loc) · 2.74 KB
/
UIAlertView+iOS8Ext.m
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
//
// UIAlertView+iOS8Ext.m
// UIAlertView+iOS8Ext
//
// Created by 王亮 on 16/8/24.
// Copyright © 2016年 王亮. All rights reserved.
//
#import "UIAlertView+iOS8Ext.h"
@implementation UIAlertView(iOS8Ext)
+ (void)showWithTag:(NSInteger)tag title:(NSString *)title message:(NSString *)message delegate:(id<UIAlertViewDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle
{
if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)) {
NSString *alertTitle = title;
if (alertTitle == nil) {
alertTitle = @"";
}
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle message:message preferredStyle:UIAlertControllerStyleAlert];
alertController.view.tag = tag;
if (cancelButtonTitle) {
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
if (delegate && [delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) {
[delegate alertView:(UIAlertView *)alertController.view clickedButtonAtIndex:0];
}
}];
[alertController addAction:cancelAction];
}
if (otherButtonTitle) {
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
if (delegate && [delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) {
[delegate alertView:(UIAlertView *)alertController.view clickedButtonAtIndex:1];
}
}];
[alertController addAction:otherAction];
}
if([delegate isKindOfClass:[UIViewController class]]) {
[((UIViewController *)delegate) presentViewController:alertController animated:YES completion:nil];
}
else {
UIViewController *vc;
if ([[UIApplication sharedApplication].windows count] > 1) {
vc = [UIApplication sharedApplication].windows[1].rootViewController;
}
else {
vc = [UIApplication sharedApplication].delegate.window.rootViewController;
}
[vc presentViewController:alertController animated:YES completion:nil];
}
}
else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitle,nil];
alertView.tag = tag;
[alertView show];
}
}
#pragma clang diagnostic pop
@end