AlertViewController的快速調(diào)用,看我就夠了。

前言

Alert 在iOS開(kāi)發(fā)中,是一個(gè)經(jīng)常用到的控件。iOS原生的API中的寫(xiě)法,我這里就不吐槽了,無(wú)論是AlertView 還是AlertViewcontroller 都需要很多代碼來(lái)調(diào)用。

網(wǎng)上有很多關(guān)于AlertView的封裝,再加上這是個(gè)已經(jīng)被廢棄的控件。我這里就不來(lái)敘述。這里提供一個(gè)我自己實(shí)現(xiàn)的AlertViewController的快速調(diào)用。

看下圖,我猜還有很多人,還在用著廢棄的Alert,忍受著 ??????


警告視圖

好了下面就直接上代碼的使用 >>>>>>

快速創(chuàng)建

代碼示范

   [XWAlert showAlertWithTitle:@"提示"
                       message:@"你瀏覽的是成人內(nèi)容,是否滿足18歲?"
                  confirmTitle:@"滿足"
                   cancelTitle:@"自動(dòng)離開(kāi)"
                preferredStyle:(UIAlertControllerStyleActionSheet/UIAlertControllerStyleAlert)
                 confirmHandle:^{
                    NSLog(@"滿足-----");
   }
                  cancleHandle:^{
                    NSLog(@"不滿足-----");

   }];
效果預(yù)覽

當(dāng)然,你還可以省略其中任何一個(gè)

自定義UIAlertAction 數(shù)量種類不限

示例1

   [XWAlert showAlertWithTitle:@"選擇題"
                        message:@"菊花一詞,為何走紅?"
                 preferredStyle:UIAlertControllerStyleAlert
                    actionMaker:^(UIAlertController *maker) {
                        
                        NSString *string1 = @"菊花臺(tái)這首歌";
                        NSString *string2 = @"陶淵明的詩(shī)詞";
                        NSString *string3 = @"象征純潔";
                        NSString *string4 = @"人體器官的形象話";
                        NSString *string5 = @"我選擇死亡";

 [maker addAlertDefaultActionWithTitle:string1 handler:^(UIAlertAction * _Nullable action) {
                            NSLog(@"你的選擇是--- %@", string1);
                        }];

                        [maker addAlertDefaultActionWithTitle:string2 handler:^(UIAlertAction * _Nullable action) {
                            NSLog(@"你的選擇是--- %@", string2);

                        }];
         
                        [maker addAlertDefaultActionWithTitle:string3 handler:^(UIAlertAction * _Nullable action) {
                            NSLog(@"你的選擇是--- %@", string3);
                        }];

                        [maker addAlertActionWithTitle:string4 actionStyle:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nullable action) {
                            NSLog(@"你的選擇是--- %@", string4);
                            
                        }];

                        [maker addAlertActionWithTitle:string5 actionStyle:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nullable action) {
                            NSLog(@"你的選擇是--- %@", string5);

                        }];
    }];
}
示例1

示例2

   [XWAlert showAlertWithTitle:@"注意"
                        message:@"按照要求填寫(xiě)信息"
                 preferredStyle:self.selectedStyle
                    actionMaker:^(UIAlertController *maker) {
                        
                        NSString *string1 = @"確定";
                        NSString *string2 = @"取消";

                        [maker addAlertDefaultActionWithTitle:string1
                                                      handler:^(UIAlertAction * _Nullable action) {
                            NSLog(@"你的選擇是--- %@", string1);
                        }];
                        

                        [maker addAlertActionWithTitle:string2
                                           actionStyle:ActionStyleCancel
                                               handler:^(UIAlertAction * _Nullable action) {
                            NSLog(@"你的選擇是--- %@", string2);
                        }];
                        

                        [maker addTextFieldWithPlaceholder:@"輸入用戶名"
                                           secureTextEntry:NO
                                                   textHandler:^(NSString * _Nullable text) {
                           NSLog(@"你輸入的用戶是--- %@",text);

                        }];
                        
                        [maker addTextFieldWithPlaceholder:@"輸入密碼"
                                           secureTextEntry:YES
                                          textFiledhandler:^(UITextField * _Nonnull textField) {
                             textField.textColor = [UIColor greenColor];
                             textField.font = [UIFont boldSystemFontOfSize:16];
                                                     }];
                    }];

示例2
 這里需要注意的是 這個(gè)方法 callback 一個(gè) UIAlertController ,*maker。 
 我為UIAlertController添加了一個(gè)便利添加UIAlertAction的Category。

第一種是添加一個(gè)默認(rèn)的AlertAction,只需要輸入標(biāo)題,然后handle業(yè)務(wù)邏輯就可以。

/**
 to add UIAlertAction with UIAlertActionStyleDefault

 @param title - the title of UIAlertAction
 @param handler - to handle your business
 */
- (void)addAlertDefaultActionWithTitle:(NSString *_Nullable)title
                               handler:(void (^_Nullable)(UIAlertAction * _Nullable  action))handler;

第二種和第一種的區(qū)別在于可以選擇樣式

/**
 to add UIAlertAction with Custom Style


 @param title - the title of UIAlertAction
 @param actionStyle - to chose UIAlertActionStyle
 @param handler - to handle your business
 */
- (void)addAlertActionWithTitle:(NSString *_Nullable)title
                    actionStyle:(UIAlertActionStyle)actionStyle
                        handler:(void (^ __nullable)(UIAlertAction * _Nullable action))handler;

第三種是添加TextFiled,可以添加占位,密文輸入。會(huì)callback 一個(gè)輸入結(jié)束后的text

/**
 to add TextField in your alert , callback the  text which  you input
 it only support in Alert Styple

 @param placeholder - set TextField's placeholder
 @param secureTextEntry - set Secure input Mode
 @param textHandler - to get text which  you input
 */
- (void)addTextFieldWithPlaceholder:(NSString *_Nullable)placeholder
                    secureTextEntry:(BOOL)secureTextEntry
                            textHandler:(TextFiledHanler _Nullable )textHandler;

第四種和第三種類似,添加textFiled;callback的是 textFiled對(duì)象本身,用于處理業(yè)務(wù)邏輯。

/**
 to add TextField in your alert, callback the  textFiled which  you built
 it only support in Alert Styple

 @param placeholder - set TextField's placeholder
 @param secureTextEntry - set Secure input Mode
 @param textFiledhandler - to handle textField which you can do anything
 */
- (void)addTextFieldWithPlaceholder:(NSString *_Nullable)placeholder
                    secureTextEntry:(BOOL)secureTextEntry
                   textFiledhandler:(void(^_Nullable)(UITextField * _Nonnull textField))textFiledhandler;

純message 自動(dòng)miss

    [XWAlert showAlertWithTitle:@"注意"
                        message:@"這是一條不要臉的彈窗"
                 preferredStyle:self.selectedStyle
                autoDismissTime:2];
純文本示例

新增 快速 創(chuàng)建 且可以自定義 style

    [XWAlert showAlertWithTitle:@"title"
                        message:@"message"
                   confirmTitle:@"default style"
                    cancelTitle:@"cancel style"
               destructiveTitle:@"destructive style"
                 preferredStyle:self.selectedStyle
                  confirmHandle:^{
                      
                    NSLog(@"------- default style");

    }
                   cancleHandle:^{
                    NSLog(@"------- cancel style");

    }
              destructiveHandle:^{
                    NSLog(@"------- destructive style");

    }];
    
示范圖

CocoaPods 安裝

pod 'XWAlert', '~> 1.3'

#import <XWAlert.h>

后記

這只是自己設(shè)計(jì)出來(lái)的一種方案,實(shí)現(xiàn)了AlertViewController的大部分功能。
在使用的過(guò)程有任何問(wèn)題都可以向我提出
如果大家有更加好的方案和思路,希望把地址發(fā)出來(lái),一起參考學(xué)習(xí)。

最后, 如果覺(jué)得對(duì)你有所幫助,還希望大家對(duì)我進(jì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)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,274評(píng)論 4 61
  • 西風(fēng)東逝,又一池荷老,露從霜降。 辭罷清秋作小雅,此意難表薄葬。 夜夜弦歌,清樽縱酒,一曲回腸蕩。 緣誰(shuí)苦笑,離別...
    滄玉閱讀 542評(píng)論 4 6
  • 這兩天早晨都在練習(xí)xdite老師的急速閱讀法,實(shí)踐下認(rèn)知學(xué)習(xí)法的相關(guān)技術(shù)。 ** Step 1 :找出你最想問(wèn)這...
    digman閱讀 308評(píng)論 0 0
  • 憶江南.江南憶 白居易 江南好,風(fēng)景舊曾諳。 日出江花紅勝火,春來(lái)江水綠如藍(lán)。 能不憶江南? 今天事情終于有了一...
    頑石的青果花園閱讀 244評(píng)論 0 2

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