Sagit.Framework For IOS 開發架構入門教程5:訊息彈窗STMsgBox,
前言:
昨天剛寫了一篇IT連創業的文章:IT連創業系列:產品設計之答題模組,(歡迎大夥關注!)
感覺好久沒寫IOS的文章了,今天趁機,來補一篇,Sagit的教程。
Sagit 開源地址:https://github.com/cyq1162/Sagit
今天主要是分享訊息彈窗功能,即STMsgBox的用法:
STMsgBox為彈窗相關的功能的源碼。
1、對外API功能調用說明:
早期的彈窗,曾經引用了MBProgressHUD;
後來,去掉了,並重寫了裡面所有的代碼。
目前對外調用的API:
typedef BOOL (^OnConfirmClick)(NSInteger btnIndex,UIAlertView* view);typedef BOOL (^OnBeforeDialogHide)(UIView* winView,UIView* clickView);typedef void (^OnBeforeShow)(UIAlertView* view);typedef void (^OnDialogShow)(UIView* winView);//!提供基礎的訊息彈窗@interface STMsgBox : NSObject+ (STMsgBox*)share;#pragma AlertView//!提示訊息-(void)prompt:(id)msg;-(void)prompt:(id)msg second:(NSInteger)second;//!彈出需要點擊確定的訊息框-(void)alert:(id)msg;-(void)alert:(id)msg title:(NSString*)title;-(void)alert:(id)msg title:(NSString *)title okText:(NSString*)okText;-(void)loading;-(void)loading:(id)text;-(void)hideLoading;//!彈出需要確認,並可執行事件的訊息框。-(void)confirm:(id)msg title:(NSString*)title click:(OnConfirmClick)click;-(void)confirm:(id)msg title:(NSString *)title click:(OnConfirmClick)click okText:(NSString*)okText;-(void)confirm:(id)msg title:(NSString *)title click:(OnConfirmClick)click okText:(NSString*)okText cancelText:(NSString*)cancelText;//!彈出一個可以(自訂)輸入內容的對話方塊-(void)input:(id)title before:(OnBeforeShow)beforeShow click:(OnConfirmClick)click okText:(NSString*)okText cancelText:(NSString*)cancelText;//!彈出自訂介面的對話方塊- (void)dialog:(OnDialogShow)dialog;- (void)dialog:(OnDialogShow)dialog beforeHide:(OnBeforeDialogHide) beforeHide;@end
2、調用方式
對於該類的調用方式,有兩種
1、在繼承自STController的控制器下,直接用[self.msgBox ...]
STController這個基類,預設實現的有兩個子類介面(msgBox:訊息彈窗、http:網路請求)
例如:
[self.msgBox prompt:@"不可為空!"];
2、在任意地方,都可以全域的調用:用[Sagit.MsgBox ...]
Sagit,是一個總類庫的起始名稱空間,很多常用功能,都是由Sagit開啟頭開始,後續會單獨介紹。
例如:
[Sagit.MsgBox prompt:@"加入黑名單成功!"];
3、項目代碼執行個體
下面,會和大夥分享 IT連App 中,應用到該相關的代碼:
A、prompt用法:發布圈子的時候,彈出提示
-(void)onRightNavBarClick:(UIBarButtonItem *)view{ if(![self isMatch:@"發布內容" name:@"topic"]) { return; } NSString *jsonStr = [@{@"TopicContent":STValue(@"topic"),@"photos":self.photos} toJson]; [self.http post:UrlTopicSet paras:@{@"topic":jsonStr} success:^(STModel *result) { if (result.success == YES) { [self.msgBox prompt:@"發布成功!"]; [self.preController reloadData]; [self stPop]; } else { [self.msgBox prompt:result.msg]; } }];}
效果:
B、alert用法:小魔術(露一手)中的穿透術,未設定參數時的提示
-(void)initUI{ [super initUI]; [self loadScreenUI]; //禁止使用者自訂圖片,顯示為使用者佈建的穿透圖片 self.userPhotos=nil; self.showSecond=2; //讀取設定的圖片 self.penPhotos=Sagit.Magic.Setting.penPhotos; if(!self.penPhotos || self.penPhotos.count==0) { [self.msgBox alert:@"請先在參數設定裡設定穿透照片!" title:@"訊息提示" okText:@"我知道了"]; [self stPop]; return; } [self ready];}
提示效果:
C、confirm 的用法:圖片長按的提示儲存
-(UIImageView*)save{ [Sagit.MsgBox confirm:@"是否儲存圖片?" title:@"訊息提示" click:^BOOL(NSInteger isOK,UIAlertView* view) { if(isOK>0) { [self.image save:^(NSError *err) { [Sagit.MsgBox prompt:!err?@"儲存成功":@"儲存失敗:儲存照片許可權被拒絕,您需要重新設定才能儲存!"]; }]; } return YES; }]; return self;}
提示效果:
D、input用法:修改密碼時的用法
-(void)updatePassword:(UITableViewCell*)cell{ [[cell.textLabel text:@"修改密碼"] onClick:^(id view) { [self.msgBox input:@"修改密碼" before:^(UIAlertView *view) { view.alertViewStyle=UIAlertViewStyleSecureTextInput; [[view textFieldAtIndex:0] keyboardType:UIKeyboardTypeNumbersAndPunctuation]; } click:^BOOL(NSInteger isOK, UIAlertView *view) { if(isOK>0) { UITextField *text=[view textFieldAtIndex:0]; if (![self isMatch:@"密碼,格式為6-16位元字或字母" value:text.text regex:RexPassword]) { return NO; } [self.http post:UrlChangePwd paras:@{@"Password":text.text} success:^(STModel *result) { if (result.success) { [self.msgBox prompt:@"密碼修改成功"]; Sagit.Global.Token=(NSString *)result.msg; }else { [self.msgBox prompt:@"密碼修改失敗"]; } }]; } return YES; } okText:@"確認密碼" cancelText:@"取消"]; }];}
效果:
E、dialog用法,自訂表單:彈出分享模組的用法
- (void)show:(ShareModel*)model after:(OnAfterShare)after beforeViewHide:(OnBeforeDialogHide)onBeforeViewHide{ [Sagit.MsgBox dialog:^(UIView *winView) { [[[[[winView addUIView:nil] width:1 height:362] backgroundColor:DeviceColor] relate:Bottom v:0] block:nil on:^(UIView* sView) { NSArray *shareImageArray = @[@"wechat2", @"wechat", @"qq", @"qq2"]; NSArray *shareTitleArray = @[@"好友", @"朋友圈", @"QQ好友", @"QQ空間"]; for (int i = 0; i < shareImageArray.count; i ++) { UIImageView *imgView= [[[[sView addImageView:nil img:shareImageArray[i]] width:126 height:126] relate:LeftTop v:(126+36)*i+72 v2:50] onClick:^(id view) { model.To=(ShareTo)i; [self send:model after:after]; [winView click];//隱藏介面 }]; [[[[[sView addLabel:nil text:shareTitleArray[i] font:24 color:@"#555555"] textAlignment:NSTextAlignmentCenter] width:126] onBottom:imgView y:22] onClick:^(id view) { [imgView click]; }]; } [[[[[sView addUIView:nil] backgroundColor:ColorWhite] width:1 height:98]relate:Bottom v:0] block:nil on:^(UIView* cancelView) { [[cancelView addLabel:nil text:@"取消" font:32 color:ColorBlack] toCenter]; }]; }]; } beforeHide:onBeforeViewHide]; }
效果:
F、dialog用法2:彈出答題中的難度
- (void)show:(NSString*)type{ [Sagit.MsgBox dialog:^(UIView *winView) { [[[[winView addImageView:nil img:@"answer_pop"] width:1 height:814] toCenter] block:nil on:^(UIImageView *cView) { //初級 [[[[cView addUIView:@"level1"] width:140 height:62] relate:LeftTop v:216 v2:344] onClick:^(UIView* view) { [self showAnswerStart:winView questionType:type dificulty:@"1"]; }]; //中級 [[[[cView addUIView:@"level2"] width:140 height:62] onRight:@"level1" x:42 ]onClick:^(UIView* view) { [self showAnswerStart:winView questionType:type dificulty:@"2"]; }]; //進階 [[[[[cView addUIView:@"level3"] width:140 height:62] onBottom:@"level2" y:60] toCenter:X] onClick:^(UIView* view) { [self showAnswerStart:winView questionType:type dificulty:@"3"]; }]; //我要出題 [[[[cView addUIView:@"level4"] width:154 height:154] relate:LeftTop v:435 v2:538] onClick:^(UIView* view) { [winView click]; ITWebViewController *wv=[ITWebViewController new]; NSString *url=[[UrlQuestionPost replace:@"{uid}" with:Sagit.Global.UserID] replace:@"{qt}" with:type]; [wv loadUrl:url]; NSString *qtName=[Sagit.Global getConfigKey:@"答題類型" value:type]; wv.defaultTitle=STString(@"我要出題: %@",qtName); wv.denyChangeTitle=YES; [self stPush:wv]; }]; }]; }];}-(void)showAnswerStart:(UIView*)view questionType:(NSString*)type dificulty:(NSString*)dificulty{ AnswerModel *model=[AnswerModel new]; model.Score=0; model.StartTime=[NSDate.beiJinDate toString]; model.AnswerCount=1; model.QuestionType=type.integerValue; model.QuestionDifficulty=dificulty.integerValue; UIViewController *con=[STNew(@"AnswerStart") key:@"answerModel" value:model] ; [self stPush:con title:nil img:nil]; [view click];}
效果:
總結:
本篇雖然介紹的是訊息彈窗,但分享的代碼,都是IT連裡完整的功能模組了。
認真掃代碼,就能發現用Sagit架構寫代碼是簡潔的不要不要的了。
Sagit架構,讓IOS開發更簡單,你值的擁有!!!