UIAlertController是iOS8出來的新方法俏让,其將系統(tǒng)原先的UIAlertView和UIActionSheet進(jìn)行了整合滤蝠。UIAlertView和UIActionSheet被廢棄也是遲早的事,為了避免以后大量修改代碼,我們需要對其進(jìn)行封裝。
#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