實現對UIAlertController和UIAlertView判斷系統後的簡單封裝

來源:互聯網
上載者:User

標籤:uialertcontroller   uialertview   ios 8   block   封裝   

iOS8之後用UIAlertController代替了UIAlertView,所以每次有需要彈窗的時候,都需要先判斷系統,最近在做的項目中彈窗較多,如果每次都判斷,真是太麻煩了,索性對UIAlertController和UIAlertView進行的封裝了,封裝在一個工具類中,在工具類中就對系統進行判斷,然後在你需要彈窗的介面直接調用這個工具類的方法就可以了,減少了代碼的耦合.

這個工具類其實也封裝的特別簡單,因為都是用的系統的,分享出來給大家參考下:

首先是.h檔案

@interface UIAlertTool : NSObject-(void)showAlertView:(UIViewController *)viewController :(NSString *)title :(NSString *)message :(NSString *)cancelButtonTitle :(NSString *)otherButtonTitle :(void (^)())confirm :(void (^)())cancle;;@end

只有這麼一個簡單的方法  把你需要在彈窗中顯示的內容以參數的形式傳入就可以了

然後是.m檔案的實現

#define IAIOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)#import "UIAlertTool.h"typedef void (^confirm)();typedef void (^cancle)();@interface UIAlertTool(){    confirm confirmParam;    cancle  cancleParam;}@end@implementation UIAlertTool-(void)showAlertView:(UIViewController *)viewController :(NSString *)title :(NSString *)message :(NSString *)cancelButtonTitle :(NSString *)otherButtonTitle :(void (^)())confirm :(void (^)())cancle{    confirmParam=confirm;    cancleParam=cancle;    if (IAIOS8) {                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];                // Create the actions.        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {             cancle();                  }];                UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {            confirm();        }];                // Add the actions.        [alertController addAction:cancelAction];        [alertController addAction:otherAction];                [viewController presentViewController:alertController animated:YES completion:nil];        }    else{        UIAlertView *TitleAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:otherButtonTitle otherButtonTitles:cancelButtonTitle,nil];        [TitleAlert show];    }    }-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    if (buttonIndex==0) {        confirmParam();    }    else{        cancleParam();    }}@end

你在彈窗中點擊確定或者取消要實現的功能寫在block裡面傳入就OK了.SO  easy吧!

實現對UIAlertController和UIAlertView判斷系統後的簡單封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.