UIAlertController是iOS8出来的新方法,其将系统原先的UIAlertView和UIActionSheet进行了整合。UIAlertView和UIActionSheet被废弃也是迟早的事,为了避免以后大量修改代码,我们需要对其进行封装。
#import <Foundation/Foundation.h>
@interface AlertViewTool : NSObject
//封装拥有两个按钮的AlertView
- (void)showAlertView:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle confirm:(void (^)())confirm cancle:(void (^)())cancle;
//简单AlertView使用
+ (void) showSimpleAlertOnViewController:(UIViewController *)viewController WithTitle:(NSString*) title message:(NSString*) message;
@end
.m文件
#import "AlertViewTool.h"
typedef void (^confirm)();
typedef void (^cancle)();
@interface AlertViewTool ()<UIAlertViewDelegate>
@property (nonatomic, copy) confirm confirmAction;
@property (nonatomic, copy) cancle cancleAction;
@end
@implementation AlertViewTool
- (void)showAlertView:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle confirm:(void (^)())confirm cancle:(void (^)())cancle {
self.confirmAction = nil;
self.cancleAction = nil;
self.confirmAction = confirm;
self.cancleAction = cancle;
if (IS_IOS8) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
// Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
cancle();
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
confirm();
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[viewController presentViewController:alertController animated:YES completion:nil];
} else{
UIAlertView *TitleAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitle,nil];
[TitleAlert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
self.cancleAction();
} else{
self.confirmAction();
}
}
//简单AlertView使用
+ (void) showSimpleAlertOnViewController:(UIViewController *)viewController WithTitle:(NSString*) title message:(NSString*) message {
if (IS_IOS8) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
// Create the actions.
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
// Add the actions.
[alertController addAction:confirmAction];
[viewController presentViewController:alertController animated:YES completion:nil];
} else{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
[alertView show];
}
}
@end