说明:

2021-10-26 版本更新:UIAlertView(已过时) 调整为:UIAlertController 实现。

文章内容中:UIAlertView* view 参数统一变更为:UIAlertController* alertController

 

前言:

Sagit 开源地址:https://github.com/cyq1162/Sagit

今天主要是分享消息弹窗功能,即STMsgBox的用法:

STMsgBox为弹窗相关的功能的源码。

1、对外API功能调用说明:

对外调用的API:

typedef void (^OnConfirmClick)(NSInteger btnIndex,UIAlertView* view);
typedef BOOL (^OnInputClick)(NSInteger btnIndex,UIAlertView* view);
typedef void (^OnMenuClick)(NSInteger btnIndex,UIActionSheet* view);
typedef BOOL (^OnBeforeDialogHide)(UIView* winView,UIView* clickView);
typedef void (^OnBeforeShow)(UIAlertView* view);
typedef void (^OnDialogShow)(UIView* winView);
//!提供基础的消息弹窗
@interface STMsgBox : NSObject
+ (STMsgBox*)share;
//!dialog 状态。
@property (nonatomic,assign) BOOL isDialoging;
#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:(OnInputClick)click okText:(NSString*)okText  cancelText:(NSString*)cancelText;
//!弹出底部菜单
-(void)menu:(OnMenuClick)click names:(id)names,...NS_REQUIRES_NIL_TERMINATION;
//!弹出自定义界面的对话框
- (void)dialog:(OnDialogShow)dialog;
- (void)dialog:(OnDialogShow)dialog beforeHide:(OnBeforeDialogHide) beforeHide;

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;
}

提示效果:

 

confirm的用法2:【当okText 为多个项时(字符串用逗号分隔)】

[self.msgBox confirm:nil title:@"模式切换" click:^void(NSInteger btnIndex, UIAlertView *view)
     {
         if(btnIndex>0)
         {
            if(Sagit.Event.onChangeMenu)
            {
                Sagit.Event.onChangeMenu(btnIndex,YES);
            }
         }
    } okText:@"爱情,学习"];//,工作

效果:

 

 

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];
}

效果:

E、底部菜单

示例代码:

    [Sagit.MsgBox menu:^void(NSInteger btnIndex, UIActionSheet *view) {
        [Sagit.MsgBox prompt:STString(@"%@,%@",@(btnIndex),view.name) ];
        
    } names:@"a",@"b", nil];

效果:

 

 

总结:

本篇虽然介绍的是消息弹窗,但分享的代码,都是IT连里完整的功能模块了。

认真扫代码,就能发现用Sagit框架写代码是简洁的不要不要的了。

Sagit框架,让IOS开发更简单,你值的拥有!!!

posted on 2018-03-27 16:39  路过秋天  阅读(1220)  评论(0编辑  收藏  举报
路过秋天