iOS開發(fā)之封裝系統(tǒng)的alertView和alertController

UIAlertController是iOS8出來的新方法俏让,其將系統(tǒng)原先的UIAlertView和UIActionSheet進(jìn)行了整合滤蝠。UIAlertView和UIActionSheet被廢棄也是遲早的事,為了避免以后大量修改代碼,我們需要對其進(jìn)行封裝。

#import <Foundation/Foundation.h>
@interface AlertViewTool : NSObject
//封裝擁有兩個按鈕的AlertView
- (void)showAlertView:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle confirm:(void (^)())confirm cancle:(void (^)())cancle;

//簡單AlertView使用
+ (void) showSimpleAlertOnViewController:(UIViewController *)viewController WithTitle:(NSString*) title message:(NSString*) message;
@end

.m文件

#import "AlertViewTool.h"

typedef void (^confirm)();
typedef void (^cancle)();

@interface AlertViewTool ()<UIAlertViewDelegate>
@property (nonatomic, copy) confirm confirmAction;
@property (nonatomic, copy) cancle cancleAction;

@end

@implementation AlertViewTool
- (void)showAlertView:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle confirm:(void (^)())confirm cancle:(void (^)())cancle {
    self.confirmAction = nil;
    self.cancleAction = nil;
    self.confirmAction = confirm;
    self.cancleAction = cancle;
    if (IS_IOS8) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        // Create the actions.
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleDefault 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:cancelButtonTitle otherButtonTitles:otherButtonTitle,nil];
        [TitleAlert show];
    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        self.cancleAction();
    } else{
        self.confirmAction();
    }
}
//簡單AlertView使用
+ (void) showSimpleAlertOnViewController:(UIViewController *)viewController WithTitle:(NSString*) title message:(NSString*) message {
    if (IS_IOS8) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        // Create the actions.
        UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        
        // Add the actions.
        [alertController addAction:confirmAction];
        [viewController presentViewController:alertController animated:YES completion:nil];
    } else{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [alertView show];
    }
    
    
}
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市芒划,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌欧穴,老刑警劉巖民逼,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異涮帘,居然都是意外死亡拼苍,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門调缨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來疮鲫,“玉大人,你說我怎么就攤上這事弦叶】》福” “怎么了?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵伤哺,是天一觀的道長燕侠。 經(jīng)常有香客問我,道長立莉,這世上最難降的妖魔是什么绢彤? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮蜓耻,結(jié)果婚禮上茫舶,老公的妹妹穿的比我還像新娘。我一直安慰自己刹淌,他們只是感情好饶氏,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布讥耗。 她就那樣靜靜地躺著,像睡著了一般嚷往。 火紅的嫁衣襯著肌膚如雪葛账。 梳的紋絲不亂的頭發(fā)上柠衅,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天皮仁,我揣著相機(jī)與錄音,去河邊找鬼菲宴。 笑死贷祈,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的喝峦。 我是一名探鬼主播势誊,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼谣蠢!你這毒婦竟也來了粟耻?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤眉踱,失蹤者是張志新(化名)和其女友劉穎挤忙,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體谈喳,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡册烈,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了婿禽。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片赏僧。...
    茶點(diǎn)故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖扭倾,靈堂內(nèi)的尸體忽然破棺而出淀零,到底是詐尸還是另有隱情,我是刑警寧澤膛壹,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布驾中,位于F島的核電站,受9級特大地震影響恢筝,放射性物質(zhì)發(fā)生泄漏哀卫。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一撬槽、第九天 我趴在偏房一處隱蔽的房頂上張望此改。 院中可真熱鬧,春花似錦侄柔、人聲如沸共啃。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽移剪。三九已至究珊,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間纵苛,已是汗流浹背剿涮。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留攻人,地道東北人取试。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像怀吻,于是被迫代替她去往敵國和親瞬浓。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評論 2 355

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

  • 咖啡是采用經(jīng)過烘焙的咖啡豆制作的飲料蓬坡,通常為熱飲猿棉,但也有作為冷飲的冰咖啡⌒伎龋咖啡是人類社會流行范圍最為廣泛的飲料之一...
    安亦年閱讀 243評論 0 5
  • 天亞通科技閱讀 266評論 0 0