沒(méi)有viewController中實(shí)現(xiàn)UIAlertController

在最新的iOS的api中,使用了UIAlertController代替了iOS7及其以下的UIAlertView,同時(shí)取消了UIAlertView的代理方法。UIAlertController在Controller中使用是十分方便的,但是不盡人意的是,在很多其他情況下,我們需要在其他環(huán)境中使用UIAlertController.

  1. 介紹UIAlertView
    UIAlertView中能在iOS7及其以下的系統(tǒng)中使用,UIAlertView官方文檔。主要是生成一個(gè)alertView,通過(guò)實(shí)現(xiàn)的它對(duì)應(yīng)的代理方法,來(lái)實(shí)現(xiàn)不同按鈕對(duì)應(yīng)的不同的業(yè)務(wù)邏輯。具體的方法可以參照Apple的文檔,很詳細(xì)。

  2. 在ViewController中實(shí)現(xiàn)UIAlertController
    UIAlertController文檔,UIAlertController主要是為了替換UIAlertView和UIActionSheet類(lèi),自iOS8之后有效。

(1).創(chuàng)建UIAlertController

+ (instancetype)alertControllerWithTitle:(NSString*)title message:  (NSString*)message preferredStyle:(UIAlertControllerStyle)preferredStyle

其中通過(guò)屬性UIAlertControllerStyle來(lái)區(qū)分創(chuàng)建的是UIAlertView類(lèi)型,還是UIActionSheet類(lèi)型。

(2).新增UIAlertAction

UIAlertController的對(duì)象是通過(guò)新增的UIAlertAction對(duì)象來(lái)進(jìn)行單擊反饋的。所以要?jiǎng)?chuàng)建UIAlertAction對(duì)象。

+ (instancetype)actionWithTitle:(NSString*)title style:(UIAlertActionStyle)style    handler:(void (^)(UIAlertAction *action))handler

之后通過(guò)

- (void)addAction:(UIAlertAction*)action

方法將Action添加到UIAlertController對(duì)象實(shí)例中。

備注:

  1. 在action的block中,就是該action觸發(fā)的處理事件(handler)。
  2. 可以通過(guò)配置action的UIAlertActionStyle屬性來(lái)展示action的樣式。

(3).顯示UIAlertController

[self presentViewController:alertController animated:YES completion:nil];
  • 沒(méi)有在 ViewController下實(shí)現(xiàn)UIAlertController

首先看一篇文章,就是介紹如何實(shí)現(xiàn)在沒(méi)有ViewController的情況下實(shí)現(xiàn)UIAlertController。

因?yàn)閁IAlertController使用只能使用ViewController的presentViewController:animated:completion的方法才能顯示。所有在沒(méi)有直接的ViewController時(shí)候,只能想法子找到一個(gè)(viewController)或者創(chuàng)建一個(gè)。如下demo

(1).首先繼承UIAlertController,定制一個(gè)自己的UIAlertController(WPSAlertController)

@interface WPSAlertController :UIAlertController

- (void)show; // 顯示,animated = NO

- (void)showAnimated:(BOOL)animated; // 顯示 animated = NO or YES

+ (void)presentOkayWithTitle:(NSString*)title message:(NSString*)message;

+ (void)presentOkayAlertWithError:(nullableNSError*)error;

@end

(2)核心方法的實(shí)現(xiàn) - (void)showAnimated:(BOOL)animated

.m文件中的實(shí)現(xiàn)如下

@interfaceWPSAlertController()

@property(nonatomic,strong)UIWindow*alertWindow;

@end
@implementation WPSAlertController

......

// 核心方法 - 自行創(chuàng)建一個(gè)rootViewController,

- (void)showAnimated:(BOOL)animated {

    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [window setBackgroundColor:[UIColor clearColor]];
    UIViewController*rootViewController = [[UIViewController alloc] init];
    [[rootViewController view] setBackgroundColor:[UIColor clearColor]];
    // set window level
    [window setWindowLevel:UIWindowLevelAlert + 1];
    [window makeKeyAndVisible];
    [self setAlertWindow:window];
    [window setRootViewController:rootViewController];

    [rootViewController presentViewController:self animated:animated completion:nil];
}


+ (void)presentOkayWithTitle:(NSString*)title message:(NSString*)message {

    WPSAlertController*alertController = [WPSAlertController        alertControllerWithTitle:title message:message      preferredStyle:UIAlertControllerStyleAlert];

    //創(chuàng)建UIAlertAction

    UIAlertAction*okayAction = [UIAlertAction actionWithTitle:@"Okay"       style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnull action) {

    // you code for okay action

}];

    [alertController addAction:okayAction];

    UIAlertAction*errorAction = [UIAlertAction actionWithTitle:@"cancle"    style:UIAlertActionStyleCancel handler:^(UIAlertAction*_Nonnull action) {

    // you code for cancle action

}];

    [alertController addAction:errorAction];
    // present the alertController with animated
    [alertControllershowAnimated:YES];

}

@end

最后,只需要在需要的地方直接調(diào)用+ (void)presentOkayWithTitle:(NSString)title message:(NSString)message方法即可。(其他個(gè)性化的都可以自行實(shí)現(xiàn))

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容