怪物背景:
事發(fā)當(dāng)天我正戴著耳機(jī)悠閑的聽著普通DISCO力试,有節(jié)奏的敲著代碼。忽然排嫌,旁邊辦公桌發(fā)出一聲巨響~ 帶著幾個(gè)字“iOS端崩潰了!”!!這聲音猶如晴天霹靂畸裳,直接傳入我的靈魂,瞬間爆炸淳地。我二話不說怖糊,當(dāng)時(shí)就拿出手機(jī)接上電腦運(yùn)行項(xiàng)目跑了一遍帅容,發(fā)現(xiàn)并沒有什么問題。然而這時(shí)產(chǎn)品經(jīng)理拿著上古至寶iPhone4s朝我走來伍伤,丰嘉。我接過手機(jī),接上電腦嚷缭,運(yùn)行一看,果然崩潰耍贾。打開“關(guān)于手機(jī)”才發(fā)現(xiàn)阅爽,這是傳說中蘊(yùn)含些許鴻蒙之氣的iOS7。
(文筆不行荐开,實(shí)在編不下去了付翁,總之就是在iOS7崩潰了,是UIAlertController不支持iOS7所以崩潰了晃听。)
發(fā)現(xiàn)這個(gè)之后我根本不慌~ 因?yàn)閱栴}十分明顯百侧,。解決方法就是判斷版本適配一下iOS7就可以了能扒。不過/////我準(zhǔn)備開始弄的時(shí)候佣渴,。初斑。懵逼了辛润。 這么逗地方不可能每個(gè)都寫個(gè)什么if else吧,這簡直就是凡人的做法见秤。封裝.... 必須得封裝砂竖。。
殺怪秘籍:
其實(shí)最簡單粗暴直接有效的方法就是自己封裝自定義一個(gè)AlertView.不過這個(gè)要看個(gè)人水平了鹃答,寫出來如果是5毛特效再帶著拙劣的人造靈根氣息乎澄,那這就沒必要了。
- - 然而我就是不太相信自己测摔。(當(dāng)然也擔(dān)心后期會(huì)有各種拓展的麻煩置济,所以放棄了自定義一個(gè)的想法)。
查閱各種資料思考之后避咆,~最后用了一種自認(rèn)為比較簡單的方法(有個(gè)地方處理不太好舟肉,也希望有大神看到,能指點(diǎn)一二查库。)
我選擇直接利用UIViewController+Category的方法路媚,在里面寫一個(gè)統(tǒng)一的彈框方法,解決這個(gè)問題樊销。
幾個(gè)顯示方法如下:
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
//? ? }
//}
(里面那個(gè)宏是為了讓代碼在Xcode中不會(huì)報(bào)警告整慎,強(qiáng)迫癥必備~脏款。有點(diǎn)懶,裤园。就直接復(fù)制項(xiàng)目的撤师,需要的就自己拿去改改。記得是寫在UIViewController+Category里的)
由于- - 確實(shí)還很菜拧揽,封裝的不太好剃盾,我傳的參數(shù)需要有buttonTitle,而且是特定的buttonTitle,規(guī)則 “文本”+“類型”= “新的文本” (意思就是文本的字符串拼接上特定的類型字符串淤袜。痒谴。。)
目前我用到了這么些類型:
#define ACTIONSTYLE_CANCEL @"-CANCLE"
#define ACTIONSTYLE_NORMAL @"-NORMAL"
#define ACTIONSTYLE_DESTRUCTIVE @"-DESTRUCTIVE"
#define ACTIONSTYLE_DISABLED @"+DISABELD"
使用的時(shí)候就是:
NSString *buttonTitle = [NSString stringWithFormat:@"取消%@",ACTIONSTYLE_CANCEL];
所以需要有一些其它的format和獲取style的方法
//這個(gè)是iOS8 設(shè)置禁用按鈕的方法
//- (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;
//? ? }
//}
//這個(gè)是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];
//? ? ? ? ? ? }
//? ? ? ? }
//? ? }
//}
//這個(gè)是格式化buttonTitle的(就是截掉那后面的類型)
//- (NSString *)formatButtonTitle:(NSString *)buttonTitle
//{
//? ? NSRange range = [buttonTitle rangeOfString:@"-"];
//? ? if (range.location != NSNotFound) {
//? ? ? ? return [buttonTitle substringToIndex:range.location];
//? ? }
//? ? return buttonTitle;
//}
//這是根據(jù)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;
//}
(好吧铡羡,看到這里可能有些人已經(jīng)準(zhǔn)備開噴了~ 沒事积蔚,來吧~。我全接下了烦周。當(dāng)然尽爆,大神意見我更愿意接~)
最后就是需要實(shí)現(xiàn)一下代理方法,读慎。然后都統(tǒng)一調(diào)用一個(gè)自己寫的方法
//- (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 實(shí)現(xiàn)一下就行了∝参可以根據(jù)message或者buttonTitle判斷來實(shí)現(xiàn)事件
//- (void)clickedAlertButtonWithMessage:(NSString *)message buttonTitle:(NSString *)buttonTitle
//{
//}
最后來一次使用方法:
NSString *buttonTitle1 = [NSString stringWithFormat:@"相機(jī)%@",ACTIONSTYLE_NORMAL];
NSString *buttonTitle2 = [NSString stringWithFormat:@"相冊%@",ACTIONSTYLE_NORMAL];
NSString *buttonTitle3 = [NSString stringWithFormat:@"取消%@",ACTIONSTYLE_CANCEL];
//self是使用的UIViewController
[self showActionSheetViewWithTitle:@"標(biāo)題" message:@“內(nèi)容” buttonTitles:@[buttonTitle1,buttonTitle2,buttonTitle3]];
記得實(shí)現(xiàn)之前的clickedAlert.....這個(gè)方法饱亿,然后就可以處理各種事件了。 ?各種隨便彈闰靴,再也不用擔(dān)心iOS7和iOS8誰支持什么的問題了彪笼。
好吧~ ?我承認(rèn)是很辣雞的方法~ ? 但是,確實(shí)解決了我當(dāng)時(shí)的問題蚂且。我也問了度娘女神~配猫,但是她這次并沒有幫到我什么。(不要給我說為什么不要谷歌什么的杏死,我就是愛度娘~)
后續(xù):
目前沒有發(fā)現(xiàn)什么問題泵肄,也挺好擴(kuò)展。淑翼。腐巢。除了那幾個(gè)buttonTitle確實(shí)惡心。玄括。冯丙。
等我收到大神指點(diǎn)~ 再來修改升級(jí)。遭京。胃惜。