iOS7/8 Alert系統(tǒng)彈框的適配

怪物背景:

事發(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í)。遭京。胃惜。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末泞莉,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子船殉,更是在濱河造成了極大的恐慌鲫趁,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件利虫,死亡現(xiàn)場離奇詭異挨厚,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)糠惫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進(jìn)店門幽崩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人寞钥,你說我怎么就攤上這事∧把。” “怎么了理郑?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長咨油。 經(jīng)常有香客問我您炉,道長,這世上最難降的妖魔是什么役电? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任赚爵,我火速辦了婚禮,結(jié)果婚禮上法瑟,老公的妹妹穿的比我還像新娘冀膝。我一直安慰自己,他們只是感情好霎挟,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布窝剖。 她就那樣靜靜地躺著,像睡著了一般酥夭。 火紅的嫁衣襯著肌膚如雪赐纱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天熬北,我揣著相機(jī)與錄音疙描,去河邊找鬼。 笑死讶隐,一個(gè)胖子當(dāng)著我的面吹牛起胰,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播巫延,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼待错,長吁一口氣:“原來是場噩夢啊……” “哼籽孙!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起火俄,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤犯建,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后瓜客,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體适瓦,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年谱仪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了玻熙。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,696評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡疯攒,死狀恐怖嗦随,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情敬尺,我是刑警寧澤枚尼,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站砂吞,受9級(jí)特大地震影響署恍,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蜻直,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一盯质、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧概而,春花似錦呼巷、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至乡范,卻和暖如春配名,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背晋辆。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工渠脉, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人瓶佳。 一個(gè)月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓芋膘,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子为朋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容