怪物背景:
事發當天我正戴著耳機悠閑的聽著普通DISCO,有節奏的敲著代碼。忽然,旁邊辦公桌發出一聲巨響~ 帶著幾個字“iOS端崩潰了!”!!這聲音猶如晴天霹靂,直接傳入我的靈魂,瞬間爆炸。我二話不說,當時就拿出手機接上電腦運行項目跑了一遍,發現并沒有什么問題。然而這時產品經理拿著上古至寶iPhone4s朝我走來,。我接過手機,接上電腦,運行一看,果然崩潰。打開“關于手機”才發現,這是傳說中蘊含些許鴻蒙之氣的iOS7。
(文筆不行,實在編不下去了,總之就是在iOS7崩潰了,是UIAlertController不支持iOS7所以崩潰了。)
發現這個之后我根本不慌~ 因為問題十分明顯,。解決方法就是判斷版本適配一下iOS7就可以了。不過/////我準備開始弄的時候,。。懵逼了。 這么逗地方不可能每個都寫個什么if else吧,這簡直就是凡人的做法。封裝.... 必須得封裝。。
殺怪秘籍:
其實最簡單粗暴直接有效的方法就是自己封裝自定義一個AlertView.不過這個要看個人水平了,寫出來如果是5毛特效再帶著拙劣的人造靈根氣息,那這就沒必要了。
- - 然而我就是不太相信自己。(當然也擔心后期會有各種拓展的麻煩,所以放棄了自定義一個的想法)。
查閱各種資料思考之后,~最后用了一種自認為比較簡單的方法(有個地方處理不太好,也希望有大神看到,能指點一二。)
我選擇直接利用UIViewController+Category的方法,在里面寫一個統一的彈框方法,解決這個問題。
幾個顯示方法如下:
1.AlertView類型的:
////彈框
//- (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message buttonTitles:(NSArray *)buttonTitles;
//{
//
//? ? if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
//
//? ? ? ? UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
//? ? ? ? for (NSInteger i = 0; i < buttonTitles.count; i++) {
//? ? ? ? ? ? NSString *tempStr = buttonTitles[i];
//? ? ? ? ? ? NSString *buttonTitleStr = [self formatButtonTitle:tempStr];
//? ? ? ? ? ? UIAlertActionStyle actionStyle = [self actionStyleWithTitle:tempStr];
//
//? ? ? ? ? ? UIAlertAction *alertAction = [UIAlertAction actionWithTitle:buttonTitleStr style:actionStyle handler:^(UIAlertAction * _Nonnull action) {
//? ? ? ? ? ? ? ? [self clickedAlertButtonWithMessage:message buttonTitle:buttonTitleStr];
//? ? ? ? ? ? }];
//? ? ? ? ? ? [alertController addAction:alertAction];
//? ? ? ? }
//? ? ? ? [self presentViewController:alertController animated:YES completion:nil];
//? ? }else{
//#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0
//
//? ? ? ? UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
//
//? ? ? ? for (NSInteger i = 0 ; i < buttonTitles.count; i++) {
//? ? ? ? ? ? NSString *tempStr = buttonTitles[i];
//? ? ? ? ? ? NSString *buttonTitleStr = [self formatButtonTitle:tempStr];
//? ? ? ? ? ? [alertView addButtonWithTitle:buttonTitleStr];
//? ? ? ? }
//? ? ? ? alertView.delegate = self;
//? ? ? ? [alertView show];
//#endif
//? ? }
//}
2. ActionSheet類型
////sheet
//- (void)showActionSheetViewWithTitle:(NSString *)title message:(NSString *)message buttonTitles:(NSArray *)buttonTitles;
//{
//? ? if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
//? ? ? ? UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleActionSheet];
//? ? ? ? for (NSInteger i = 0; i < buttonTitles.count; i++) {
//? ? ? ? ? ? NSString *tempStr = buttonTitles[i];
//? ? ? ? ? ? NSString *buttonTitleStr = [self formatButtonTitle:tempStr];
//? ? ? ? ? ? UIAlertActionStyle actionStyle = [self actionStyleWithTitle:tempStr];
//? ? ? ? ? ? UIAlertAction *alertAction = [UIAlertAction actionWithTitle:buttonTitleStr style:actionStyle handler:^(UIAlertAction * _Nonnull action) {
//? ? ? ? ? ? ? ? [self clickedAlertButtonWithMessage:message buttonTitle:buttonTitleStr];
//? ? ? ? ? ? }];
//? ? ? ? ? ? [self disabledButtonWithAlertAction:alertAction];
//? ? ? ? ? ? [alertController addAction:alertAction];
//? ? ? ? }
////? ? ? ? [self disabledButtonWithSuperView:alertController.view];
//? ? ? ? [self presentViewController:alertController animated:YES completion:nil];
//? ? }else{
//#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0
//? ? ? ? UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
////? ? ? ? NSInteger cancelIndex = 0;
//? ? ? ? for (NSInteger i = 0 ; i < buttonTitles.count; i++) {
//? ? ? ? ? ? NSString *tempStr = buttonTitles[i];
//? ? ? ? ? ? NSString *buttonTitleStr = [self formatButtonTitle:tempStr];
//? ? ? ? ? ? if ([self actionStyleWithTitle:tempStr] == UIAlertActionStyleDestructive) {
//? ? ? ? ? ? ? ? actionSheet.destructiveButtonIndex = i;
//? ? ? ? ? ? }
//? ? ? ? ? ? if (![buttonTitleStr isEqualToString:@"取消"]) {
//? ? ? ? ? ? ? ? [actionSheet addButtonWithTitle:buttonTitleStr];
//? ? ? ? ? ? }
//? ? ? ? }
//? ? ? ? [actionSheet addButtonWithTitle:@"取消"];
//? ? ? ? actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;
//? ? ? ? [self disabledButtonWithSuperView:actionSheet];
//? ? ? ? [actionSheet showInView:WINDOW];
//#endif
//? ? }
//}
(里面那個宏是為了讓代碼在Xcode中不會報警告,強迫癥必備~。有點懶,。就直接復制項目的,需要的就自己拿去改改。記得是寫在UIViewController+Category里的)
由于- - 確實還很菜,封裝的不太好,我傳的參數需要有buttonTitle,而且是特定的buttonTitle,規則 “文本”+“類型”= “新的文本” (意思就是文本的字符串拼接上特定的類型字符串。。。)
目前我用到了這么些類型:
#define ACTIONSTYLE_CANCEL @"-CANCLE"
#define ACTIONSTYLE_NORMAL @"-NORMAL"
#define ACTIONSTYLE_DESTRUCTIVE @"-DESTRUCTIVE"
#define ACTIONSTYLE_DISABLED @"+DISABELD"
使用的時候就是:
NSString *buttonTitle = [NSString stringWithFormat:@"取消%@",ACTIONSTYLE_CANCEL];
所以需要有一些其它的format和獲取style的方法
//這個是iOS8 設置禁用按鈕的方法
//- (void)disabledButtonWithAlertAction:(UIAlertAction *)alertAction
//{
//? ? NSString *buttonTitle = alertAction.title;
//? ? NSRange range = [buttonTitle rangeOfString:@"+"];
//? ? if (range.location != NSNotFound) {
//? ? ? ? NSRange rangeCancel = [buttonTitle rangeOfString:ACTIONSTYLE_DISABLED];
//? ? ? ? if (rangeCancel.location != NSNotFound) {
//? ? ? ? ? ? [alertAction setValue:_999999 forKey:@"titleTextColor"];
//? ? ? ? }
//? ? ? ? [alertAction setValue:[buttonTitle substringToIndex:range.location] forKey:@"title"];
//
//? ? ? ? alertAction.enabled = NO;
//? ? }
//}
//這個是iOS7的
//- (void)disabledButtonWithSuperView:(UIView *)superView{
//? ? for (UIView *subView in superView.subviews) {
//? ? ? ? if (subView.subviews.count > 0) {
//? ? ? ? ? ? [self disabledButtonWithSuperView:subView];
//? ? ? ? }
//? ? ? ? if ([subView isKindOfClass:[UIButton class]]) {
//? ? ? ? ? ? UIButton *button = (UIButton*)subView;
//? ? ? ? ? ? NSRange range = [button.titleLabel.text rangeOfString:@"+"];
//? ? ? ? ? ? if (range.location == NSNotFound) {
////? ? ? ? ? ? ? ? [button setTitle:[button.titleLabel.text substringToIndex:range.location] forState:UIControlStateNormal];
//? ? ? ? ? ? ? ? [button setTitleColor:_333333 forState:UIControlStateNormal];
//? ? ? ? ? ? }
//? ? ? ? }
//? ? }
//}
//這個是格式化buttonTitle的(就是截掉那后面的類型)
//- (NSString *)formatButtonTitle:(NSString *)buttonTitle
//{
//? ? NSRange range = [buttonTitle rangeOfString:@"-"];
//? ? if (range.location != NSNotFound) {
//? ? ? ? return [buttonTitle substringToIndex:range.location];
//? ? }
//? ? return buttonTitle;
//}
//這是根據buttonTitle返回Action類型
//- (UIAlertActionStyle)actionStyleWithTitle:(NSString *)title
//{
//? ? NSRange rangeCancel = [title rangeOfString:ACTIONSTYLE_CANCEL];
//? ? if (rangeCancel.location != NSNotFound) {
//? ? ? ? return UIAlertActionStyleCancel;
//? ? }
//? ? NSRange rangeNormal = [title rangeOfString:ACTIONSTYLE_DESTRUCTIVE];
//? ? if (rangeNormal.location != NSNotFound) {
//? ? ? ? return UIAlertActionStyleDestructive;
//? ? }
//
//? ? return UIAlertActionStyleDefault;
//}
(好吧,看到這里可能有些人已經準備開噴了~ 沒事,來吧~。我全接下了。當然,大神意見我更愿意接~)
最后就是需要實現一下代理方法,。然后都統一調用一個自己寫的方法
//- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//? ? [self clickedAlertButtonWithMessage:nil buttonTitle:[actionSheet buttonTitleAtIndex:buttonIndex]];
//}
//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//? ? [self clickedAlertButtonWithMessage:alertView.message buttonTitle:[alertView buttonTitleAtIndex:buttonIndex]];
//}
//自己寫的方法,在需要的UIViewController 實現一下就行了。可以根據message或者buttonTitle判斷來實現事件
//- (void)clickedAlertButtonWithMessage:(NSString *)message buttonTitle:(NSString *)buttonTitle
//{
//}
最后來一次使用方法:
NSString *buttonTitle1 = [NSString stringWithFormat:@"相機%@",ACTIONSTYLE_NORMAL];
NSString *buttonTitle2 = [NSString stringWithFormat:@"相冊%@",ACTIONSTYLE_NORMAL];
NSString *buttonTitle3 = [NSString stringWithFormat:@"取消%@",ACTIONSTYLE_CANCEL];
//self是使用的UIViewController
[self showActionSheetViewWithTitle:@"標題" message:@“內容” buttonTitles:@[buttonTitle1,buttonTitle2,buttonTitle3]];
記得實現之前的clickedAlert.....這個方法,然后就可以處理各種事件了。 ?各種隨便彈,再也不用擔心iOS7和iOS8誰支持什么的問題了。
好吧~ ?我承認是很辣雞的方法~ ? 但是,確實解決了我當時的問題。我也問了度娘女神~,但是她這次并沒有幫到我什么。(不要給我說為什么不要谷歌什么的,我就是愛度娘~)
后續:
目前沒有發現什么問題,也挺好擴展。。。除了那幾個buttonTitle確實惡心。。。
等我收到大神指點~ 再來修改升級。。。