用一個(gè)簡潔的方式使用UIAlertController青柄。
一個(gè)iPhone X的適配讓樓主受盡了自定義的苦,使用系統(tǒng)API多好松忍,所以在樓主不懈的努力下潭枣,終于和組長達(dá)成一致:逐步用系統(tǒng)控件替換代碼里面的自定義控件震放,第一個(gè)挨刀的就是 BlockAlertsAnd-ActionSheets,不是樓主不喜歡它或者它寫的不好宾毒,而是因?yàn)檫@個(gè)好替換,可是啊殿遂,樓主維護(hù)的App代碼量大概在100萬行左右伍俘,全都寫成系統(tǒng)的也會吐靶靶俊勉躺!癌瘾,下面先介紹下系統(tǒng)的樣子。
iOS8.0饵溅,蘋果開放:UIAlertController 并在之后兩個(gè)版本放棄了對UIAlertView(9.0)和 UIActionSheet(8.3)的維護(hù)妨退。
NS_CLASS_DEPRECATED_IOS(2_0, 9_0, "UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead") __TVOS_PROHIBITED
@interface UIAlertView : UIView
@end
NS_CLASS_DEPRECATED_IOS(2_0, 8_3, "UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead") __TVOS_PROHIBITED
@interface UIActionSheet : UIView
@end
UIAlertController 是UIAlertView &UIActionSheet的合集,通過UIAlertControllerStyle來確定是Alert or ActionSheet
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
- (void)addAction:(UIAlertAction *)action;
@property (nonatomic, readonly) NSArray<UIAlertAction *> *actions;
@property (nonatomic, strong, nullable) UIAlertAction *preferredAction NS_AVAILABLE_IOS(9_0);
- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
@property (nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;
@property (nullable, nonatomic, copy) NSString *title;
@property (nullable, nonatomic, copy) NSString *message;
@property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;
@end
UIAlertController 要求初始化的時(shí)候就指明類型(preferredStyle 是 readonly)蜕企,盡管API設(shè)計(jì)的相當(dāng)簡單咬荷,但是用起來總是有點(diǎn)不太舒服,如下:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:style];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"OK");
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"OK");
}];
[alertController addAction:okAction];
[alertController addAction:cancelAction];
[self /** self isKindof:UIViewController */presentViewController:alertController animated:YES /** must be YES*/ completion:nil];
樓主想哭轻掩,可是自己找的鍋幸乒,累死也要搞定,所以就著手開始處理唇牧,整了3個(gè)我就開始覺得不是辦法了罕扎,想了下,樓主決定動手改造下系統(tǒng)API丐重,樓主不會打自己臉的腔召,所以也不是瞎折騰,也不是自定義扮惦,只是改了系統(tǒng)API的調(diào)用方式臀蛛。
其實(shí)還是系統(tǒng)的!Q旅邸W瞧汀!
其實(shí)還是系統(tǒng)的Tチ臁B帐痢!氏堤!
其實(shí)還是系統(tǒng)的I尘!J笮狻闪檬!
好處是你不用太麻煩new一堆東西,也不用一次又一次addAction,只用一個(gè)點(diǎn)就可以進(jìn)行下一步购笆,基本上一氣呵成粗悯。
下面上一段代碼看看怎么使用!
[KDotAlert alert].format(@"Cancel", @"There is Cancel")
.action(@"OK", ^(UIAlertAction * _Nonnull action) {
[self showMore:@"OK"];
}).cancel(@"Cancel", nil).show(self, nil);
至于:[self showMore:@"OK"]; self是安全的同欠,不需要你加weak
看下定義:
NS_CLASS_AVAILABLE_IOS(8_0) @interface KDotAlert : NSObject
@property (nonatomic, strong, readonly) KDotAlertFormat format; // format title & message
@property (nonatomic, strong, readonly) NSString * _Nullable title; // title
@property (nonatomic, strong, readonly) NSString * _Nullable message; // message
@property (nonatomic, assign, readonly) UIAlertControllerStyle preferredStyle; // The style of the alert controller.
@property (nonatomic, strong, readonly) KDotAlertAction action; // Attaches an action object to the alert or action sheet. default style;
@property (nonatomic, strong, readonly) KDotAlertAction cancel; // ... cancel style;
@property (nonatomic, strong, readonly) KDotAlertAction destructive;// ... destructive style;
// The actions that the user can take in response to the alert or action sheet.
@property (nonatomic, strong, readonly) NSArray<UIAlertAction *> * _Nullable actions;
// Make the preferred action for the user to take from an alert.
@property (nonatomic, strong, readonly) KDotAlertPreferred preferred NS_AVAILABLE_IOS(9_0);
@property (nonatomic, strong, readonly) KDotAlertTextField textField; // Adds a text field to an alert;
@property (nonatomic, strong, readonly) NSArray<UITextField *> * _Nullable textFields; // The array of text fields displayed by the alert.
@property (nonatomic, strong, readonly) KDotAlertShow show; // show with viewController
+ (instancetype _Nonnull )alert;
+ (instancetype _Nonnull )actionSheet;
@end
基本上支持了所有系統(tǒng)API...
算了样傍,不吹了,上Github連接;
Crazy凡
2017-11-11