iOS UIAlertController的簡(jiǎn)單應(yīng)用和封裝

[下載鏈接]https://git.oschina.net/ioszxh/iOS-UIAlertController

1.創(chuàng)建一個(gè)單列管理AlertController。

+ (AlertManage *)shareManager
{
    static AlertManage *managerInstance = nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        managerInstance = [[self alloc] init];
    });
    return managerInstance;
}

2.創(chuàng)建一個(gè)方法來初始化AlertController,這里用了一個(gè)OC中NS_REQUIRES_NIL_TERMINATION宏定義,其中UIAlertControllerStyle是AlertController的兩種類型UIAlertControllerStyleActionSheet和UIAlertControllerStyleAlert。

- (AlertMananger *)creatAlertWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle cancelTitle:(NSString *)canceTitle otherTitle:(NSString *)otherTitle,...NS_REQUIRES_NIL_TERMINATION{
    
    self.alertCol = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
    
    NSString *actionTitle = nil;
    va_list args;//用于指向第一個(gè)參數(shù)
    self.actionTitles = [NSMutableArray array];
    [self.actionTitles addObject:canceTitle];
    if (otherTitle) {
        [self.actionTitles addObject:otherTitle];
        va_start(args, otherTitle);//對(duì)args進(jìn)行初始化,讓它指向可變參數(shù)表里面的第一個(gè)參數(shù)
        while ((actionTitle = va_arg(args, NSString*))) {
            
            [self.actionTitles addObject:actionTitle];
            
        }
        va_end(args);
    }
    [self buildCancelAction];
    [self buildOtherAction];
    
    return [AlertMananger shareManager];
}

3.創(chuàng)建cancelAction

NSString *cancelTitle = self.actionTitles[0];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        self.indexBlock(0);
        
    }];
    [self.alertCol addAction:cancelAction];

4.創(chuàng)建其他按鈕

- (void)buildOtherAction{
    
    for (int i = 0 ; i < self.actionTitles.count; i++) {
        if (i == 0)continue;
        NSString *actionTitle = self.actionTitles[i];
        UIAlertAction *action = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            if (self.indexBlock) {
                
                self.indexBlock(i);
            }
            
        }];
        
        [self.alertCol addAction:action];
    }
    
}

5.傳入一個(gè)ViewController來展示AlertController,通過block來回調(diào)按鈕的點(diǎn)擊。

- (void)showWithViewController:(UIViewController *)viewController IndexBlock:(AlertIndexBlock)indexBlock{
    
    if (indexBlock) {
        self.indexBlock = indexBlock;
    }
    
    [viewController presentViewController:self.alertCol animated:NO completion:^{
        
    }];
}

第一次寫簡(jiǎn)書,有什么不對(duì)的地方請(qǐng)多多指正,謝謝支持!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • iOS 8的新特性之一就是讓接口更有適應(yīng)性、更靈活,因此許多視圖控制器的實(shí)現(xiàn)方式發(fā)生了巨大的變化。全新的UIPre...
    烏拉拉zzZ閱讀 945評(píng)論 0 2
  • iOS 8的新特性之一就是讓接口更有適應(yīng)性、更靈活,因此許多視圖控制器的實(shí)現(xiàn)方式發(fā)生了巨大的變化。全新的UIPre...
    Tank丶Farmer閱讀 2,118評(píng)論 2 4
  • 轉(zhuǎn)載 http://www.cocoachina.com/ios/20141126/10320.html如果侵權(quán),...
    小劉_假裝是個(gè)程序員閱讀 663評(píng)論 0 0
  • iOS 8之后蘋果推薦我們使用的系統(tǒng)自帶的提示框是UIAlertController。UIAlertControl...
    SPIREJ閱讀 542評(píng)論 0 2
  • 當(dāng)你的應(yīng)用程序需要向用戶呈現(xiàn)重要信息,或提示用戶重要選擇時(shí),可以使用警告框(Alert View)和操作表(Act...
    pro648閱讀 62,930評(píng)論 3 47