前言
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(@"不滿足-----");
}];
當(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);
}];
}];
}
示例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];
}];
}];
這里需要注意的是 這個(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)行支持榛斯。????