初識(shí)OC,對(duì)封裝簡(jiǎn)直一竅不通,只能先從簡(jiǎn)單的控件開始學(xué)起,下面是封裝的一個(gè)UIAlertViewController分為三種模式:
.h文件
>////? AlertControllerTool.h//? 測(cè)試篇////? Created by HR on 16/12/13.//? Copyright ? 2016年 dawenkeji. All rights reserved.//#import#import@interface AlertControllerTool : NSObject
//沒有取消按鈕(確認(rèn)后無跳轉(zhuǎn))
+ (UIAlertController *)alertMessage:(NSString *)message confirmHandler:(void(^)(UIAlertAction *))confirmActionHandle viewController:(UIViewController *)vc;
//沒有取消按鈕(確認(rèn)后有跳轉(zhuǎn))
+ (UIAlertController *)alertTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle *)preferredStyle confirmHandler:(void(^)(UIAlertAction *))confirmActionHandle viewController:(UIViewController *)vc;
//有取消按鈕
+ (UIViewController *)alertTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle *)preferredStyle confirmHander:(void(^)(UIAlertAction *))confirmActionHandle cancleHander:(void(^)(UIAlertAction *))cancleActionHandle viewController:(UIViewController *)vc;
@end
.m文件
>//
#import "AlertControllerTool.h"
@implementation AlertControllerTool
//沒有取消按鈕(確認(rèn)后無跳轉(zhuǎn))
+ (UIAlertController *)alertMessage:(NSString *)message confirmHandler:(void(^)(UIAlertAction *))confirmActionHandle viewController:(UIViewController *)vc{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:
@"溫馨提示" message:
message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:
@"確定"
style:
UIAlertActionStyleDefault
handler:
confirmActionHandle];
[alert addAction:confirmAction];
[vc presentViewController:alert animated:YES completion:nil];
return alert;
}
9528--0001
//沒有取消按鈕(確認(rèn)后有跳轉(zhuǎn))
+ (UIAlertController *)alertTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle *)preferredStyle confirmHandler:(void(^)(UIAlertAction *))confirmActionHandle viewController:(UIViewController *)vc{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:
title message:
message preferredStyle:
*preferredStyle];
UIAlertAction *action = [UIAlertAction actionWithTitle:
@"確定" style:UIAlertActionStyleDefault? handler:confirmActionHandle];
[alert addAction:action];
[vc presentViewController:alert animated:YES completion:nil];
return alert;
}
//有取消按鈕
+ (UIViewController *)alertTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle *)preferredStyle confirmHander:(void(^)(UIAlertAction *))confirmActionHandle cancleHander:(void(^)(UIAlertAction *))cancleActionHandle viewController:(UIViewController *)vc{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:
title message:
message preferredStyle:
*preferredStyle];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:confirmActionHandle];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:cancleActionHandle];
[alert addAction:action1];
[alert addAction:action2];
[vc presentViewController:alert animated:YES completion:nil];
return alert;
}
@end