Skip to content

Commit

Permalink
MVP架构模式模拟登录
Browse files Browse the repository at this point in the history
  • Loading branch information
BigShow1949 committed Aug 11, 2017
1 parent 3d37fcb commit e163b25
Show file tree
Hide file tree
Showing 17 changed files with 291 additions and 3 deletions.
Binary file modified .DS_Store
Binary file not shown.
34 changes: 34 additions & 0 deletions BigShow1949.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
1841F8461DEEBD2C00EAA812 /* Business.m in Sources */ = {isa = PBXBuildFile; fileRef = 1841F8441DEEBD2C00EAA812 /* Business.m */; };
1841F8491DEEBE1000EAA812 /* Student_MC.m in Sources */ = {isa = PBXBuildFile; fileRef = 1841F8481DEEBE1000EAA812 /* Student_MC.m */; };
1841F84C1DEEBF4600EAA812 /* Dancer_MC.m in Sources */ = {isa = PBXBuildFile; fileRef = 1841F84B1DEEBF4600EAA812 /* Dancer_MC.m */; };
18494D441F3D4CEE00BFCC80 /* MVPLoginViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18494D431F3D4CEE00BFCC80 /* MVPLoginViewController.m */; };
18494D471F3D4D4300BFCC80 /* LoginModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 18494D461F3D4D4300BFCC80 /* LoginModel.m */; };
18494D4A1F3D4D5D00BFCC80 /* HttpUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 18494D491F3D4D5D00BFCC80 /* HttpUtils.m */; };
18494D501F3D4E5100BFCC80 /* LoginPresenter.m in Sources */ = {isa = PBXBuildFile; fileRef = 18494D4F1F3D4E5100BFCC80 /* LoginPresenter.m */; };
1873D77B1D8021AE008C2D1D /* img1.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 1873D77A1D8021AE008C2D1D /* img1.jpg */; };
187C28CE1DC04B1C00E77B2F /* CATransformViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 187C28CD1DC04B1C00E77B2F /* CATransformViewController.m */; };
187C28D01DC04BA600E77B2F /* CATransformViewController.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 187C28CF1DC04BA600E77B2F /* CATransformViewController.storyboard */; };
Expand Down Expand Up @@ -773,6 +777,15 @@
1841F8481DEEBE1000EAA812 /* Student_MC.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Student_MC.m; sourceTree = "<group>"; };
1841F84A1DEEBF4600EAA812 /* Dancer_MC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Dancer_MC.h; sourceTree = "<group>"; };
1841F84B1DEEBF4600EAA812 /* Dancer_MC.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Dancer_MC.m; sourceTree = "<group>"; };
18494D421F3D4CEE00BFCC80 /* MVPLoginViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MVPLoginViewController.h; sourceTree = "<group>"; };
18494D431F3D4CEE00BFCC80 /* MVPLoginViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MVPLoginViewController.m; sourceTree = "<group>"; };
18494D451F3D4D4300BFCC80 /* LoginModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LoginModel.h; sourceTree = "<group>"; };
18494D461F3D4D4300BFCC80 /* LoginModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LoginModel.m; sourceTree = "<group>"; };
18494D481F3D4D5D00BFCC80 /* HttpUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HttpUtils.h; sourceTree = "<group>"; };
18494D491F3D4D5D00BFCC80 /* HttpUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HttpUtils.m; sourceTree = "<group>"; };
18494D4B1F3D4DE400BFCC80 /* LoginViewProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LoginViewProtocol.h; sourceTree = "<group>"; };
18494D4E1F3D4E5100BFCC80 /* LoginPresenter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LoginPresenter.h; sourceTree = "<group>"; };
18494D4F1F3D4E5100BFCC80 /* LoginPresenter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LoginPresenter.m; sourceTree = "<group>"; };
1873D77A1D8021AE008C2D1D /* img1.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = img1.jpg; sourceTree = "<group>"; };
187C28CC1DC04B1C00E77B2F /* CATransformViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CATransformViewController.h; sourceTree = "<group>"; };
187C28CD1DC04B1C00E77B2F /* CATransformViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CATransformViewController.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2131,6 +2144,22 @@
path = MultipleProtocol;
sourceTree = "<group>";
};
18494D411F3D4CC800BFCC80 /* MVP_Login */ = {
isa = PBXGroup;
children = (
18494D481F3D4D5D00BFCC80 /* HttpUtils.h */,
18494D491F3D4D5D00BFCC80 /* HttpUtils.m */,
18494D421F3D4CEE00BFCC80 /* MVPLoginViewController.h */,
18494D431F3D4CEE00BFCC80 /* MVPLoginViewController.m */,
18494D451F3D4D4300BFCC80 /* LoginModel.h */,
18494D461F3D4D4300BFCC80 /* LoginModel.m */,
18494D4B1F3D4DE400BFCC80 /* LoginViewProtocol.h */,
18494D4E1F3D4E5100BFCC80 /* LoginPresenter.h */,
18494D4F1F3D4E5100BFCC80 /* LoginPresenter.m */,
);
path = MVP_Login;
sourceTree = "<group>";
};
18B482301DAE3E2600ACD4BA /* Classes */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -4286,6 +4315,7 @@
18B485D91DAE3E2700ACD4BA /* 12 - DesignPattern(设计模式) */ = {
isa = PBXGroup;
children = (
18494D411F3D4CC800BFCC80 /* MVP_Login */,
18B485DA1DAE3E2700ACD4BA /* MVVM_coderyi */,
18B485F21DAE3E2700ACD4BA /* MyDataSource */,
18B486071DAE3E2700ACD4BA /* YFDesignPatternViewController.h */,
Expand Down Expand Up @@ -5380,7 +5410,9 @@
18B488031DAE3E2700ACD4BA /* SYEmotion.m in Sources */,
18B489391DAE3E2800ACD4BA /* UIButton+AFNetworking.m in Sources */,
18B487B61DAE3E2700ACD4BA /* RZTransitionsManager.m in Sources */,
18494D501F3D4E5100BFCC80 /* LoginPresenter.m in Sources */,
18B487541DAE3E2700ACD4BA /* YFTagsCloudViewController.m in Sources */,
18494D4A1F3D4D5D00BFCC80 /* HttpUtils.m in Sources */,
18B488301DAE3E2800ACD4BA /* YFEmitterRainCell.m in Sources */,
18B488181DAE3E2700ACD4BA /* YFAnimationVC02.m in Sources */,
18B488CD1DAE3E2800ACD4BA /* YFAnswerViewController.m in Sources */,
Expand Down Expand Up @@ -5512,6 +5544,7 @@
18B488221DAE3E2800ACD4BA /* YFDragRadialMenu.m in Sources */,
18B487A81DAE3E2700ACD4BA /* ITRSecondViewController.m in Sources */,
18B487D51DAE3E2700ACD4BA /* ZWIntroductionViewController.m in Sources */,
18494D441F3D4CEE00BFCC80 /* MVPLoginViewController.m in Sources */,
18B489251DAE3E2800ACD4BA /* YFMonkeyKingVC.m in Sources */,
18B489221DAE3E2800ACD4BA /* YFEightQueensVC.m in Sources */,
18B489141DAE3E2800ACD4BA /* DataUpdateView.m in Sources */,
Expand Down Expand Up @@ -5565,6 +5598,7 @@
18B4891B1DAE3E2800ACD4BA /* YFDataPersistenceViewController.m in Sources */,
18B487DA1DAE3E2700ACD4BA /* Case3ViewController.m in Sources */,
18B488561DAE3E2800ACD4BA /* YFAnimationsViewController.m in Sources */,
18494D471F3D4D4300BFCC80 /* LoginModel.m in Sources */,
18B4896A1DAE3E2800ACD4BA /* UIButton+WebCache.m in Sources */,
18B4879B1DAE3E2700ACD4BA /* JCFlipViewAnimationHelper.m in Sources */,
18B488041DAE3E2700ACD4BA /* SYEmotionTool.m in Sources */,
Expand Down
Binary file not shown.
Binary file modified BigShow1949/Classes/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// HttpUtils.h
// Dream_Architect_MVP_OC
//
// Created by Apple on 2017/2/7.
// Copyright © 2017年 乔布永. All rights reserved.
//

#import <Foundation/Foundation.h>
typedef void(^Callback) (NSString* result);
@interface HttpUtils : NSObject
+ (void)postWithName:(NSString*)name pwd:(NSString*)pwd callback:(Callback)callback;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// HttpUtils.m
// Dream_Architect_MVP_OC
//
// Created by Apple on 2017/2/7.
// Copyright © 2017年 乔布永. All rights reserved.
//

#import "HttpUtils.h"

@implementation HttpUtils
+ (void)postWithName:(NSString*)name pwd:(NSString*)pwd callback:(Callback)callback{
//发起网络请求
//第三方框架(这地方网络框架既能调用系统,又能调用第三方的框架)
//希望做一个这样的处理(方便后期的维护和扩展)
//直接调用系统吧
//第一步:
NSURL *url = [NSURL URLWithString:@"http://42.120.11.155/index.php?m=home&c=user&a=login"];
//第二步:
NSMutableURLRequest * request = [[NSMutableURLRequest alloc]initWithURL:url];
//第三步:
request.HTTPMethod = @"POST";
NSString *params = [NSString stringWithFormat:@"mobile=%@&password=%@",name,pwd];
request.HTTPBody = [params dataUsingEncoding:NSUTF8StringEncoding];
//第四步:
//创建请求会话
NSURLSession *session = [NSURLSession sharedSession];
//第五步:创建请求任务
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//第七步:处理请求结果
if (error!=nil) {
NSLog(@"登录失败");
}else{

NSLog(@"登录成功");
//回调
NSString *result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
callback(result);

}
}];
// 第六步:执行任务
[task resume];

}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// LoginModel.h
// BigShow1949
//
// Created by apple on 17/8/11.
// Copyright © 2017年 BigShowCompany. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "HttpUtils.h"

//M层(数据层,数据库,网络,文件等...)
@interface LoginModel : NSObject
//业务方法
- (void)loginWithName:(NSString*)name pwd:(NSString*)pwd callback:(Callback)callback;
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// LoginModel.m
// BigShow1949
//
// Created by apple on 17/8/11.
// Copyright © 2017年 BigShowCompany. All rights reserved.
//

#import "LoginModel.h"

@implementation LoginModel
- (void)loginWithName:(NSString*)name pwd:(NSString*)pwd callback:(Callback)callback{
//实现功能
//例如:访问网络?访问数据库?
//数据曾划分了模块()
[HttpUtils postWithName:name pwd:pwd callback:^(NSString *result) {
//解析json ,xml数据
//然后保存数据库
//中间省略100行代码
callback(result);//返回数据回调
}];
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// LoginPresenter.h
// BigShow1949
//
// Created by apple on 17/8/11.
// Copyright © 2017年 BigShowCompany. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "LoginViewProtocol.h"
#import "LoginModel.h"
// P层
//中介(用于关联M层和V层)
@interface LoginPresenter : NSObject

//提供一个业务方法
- (void)loginWithName:(NSString*)name pwd:(NSString*)pwd;
- (void)attachView:(id<LoginViewProtocol>)loginView;
- (void)detachView;
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// LoginPresenter.m
// BigShow1949
//
// Created by apple on 17/8/11.
// Copyright © 2017年 BigShowCompany. All rights reserved.
//

#import "LoginPresenter.h"


//P是中介(职责是用于关联M和V)
//P层需要:持有M层的引用和V层的引用(OOP)思想
@interface LoginPresenter ()
@property (nonatomic,strong) LoginModel *loginModel;
@property (nonatomic,strong) id<LoginViewProtocol> loginView;
@end


@implementation LoginPresenter
- (instancetype)init{
self = [super init];
if (self) {
//持有M层的引用
_loginModel = [[LoginModel alloc]init];
}
return self;
}
//提供绑定V层方法
//绑定
- (void)attachView:(id<LoginViewProtocol>)loginView{
_loginView = loginView;
}
//解除绑定
- (void)detachView{
_loginView = nil;
}
//实现业务方法
- (void)loginWithName:(NSString*)name pwd:(NSString*)pwd{
[_loginModel loginWithName:name pwd:pwd callback:^(NSString *result) {
if (_loginView != nil) {
[_loginView onLoginResult:result];
}
}];

}


@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// LoginViewProtocal.h
// BigShow1949
//
// Created by apple on 17/8/11.
// Copyright © 2017年 BigShowCompany. All rights reserved.
//

#import <Foundation/Foundation.h>

//V层
@protocol LoginViewProtocol <NSObject>

- (void)onLoginResult:(NSString*)result;
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// MVPLoginViewController.h
// BigShow1949
//
// Created by apple on 17/8/11.
// Copyright © 2017年 BigShowCompany. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MVPLoginViewController : UIViewController

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// MVPLoginViewController.m
// BigShow1949
//
// Created by apple on 17/8/11.
// Copyright © 2017年 BigShowCompany. All rights reserved.
//

#import "MVPLoginViewController.h"
#import "LoginViewProtocol.h"
#import "LoginPresenter.h"

@interface MVPLoginViewController ()<LoginViewProtocol>
@property (nonatomic,strong) LoginPresenter* presenter;

@end

@implementation MVPLoginViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];

// 模拟登录按钮点击
UIButton *redBtn = [[UIButton alloc] init];
redBtn.frame = CGRectMake(100, 100, 100, 100);
[redBtn setTitle:@"登 录" forState:UIControlStateNormal];
[redBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
redBtn.backgroundColor = [UIColor redColor];
[self.view addSubview:redBtn];

}

- (void)loginClick {
_presenter = [[LoginPresenter alloc ]init];
[_presenter attachView:self];
//程序一旦运行立马执行请求(测试)(按钮或者事件)
[_presenter loginWithName:@"18842693828" pwd:@"123456"];
}

- (void)onLoginResult:(NSString *)result{

NSLog(@"返回结果 = %@",result);

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
[_presenter detachView];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@

@interface MyDataSourceViewController : UIViewController

/*
内容不多, 就不创建文件了, 全部都放在一个文件目录下
*/
@end
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ - (UITableView *)tableView {

if (!_tableView) {
UITableView *tableView = [[UITableView alloc] init];
tableView.frame = CGRectMake(0, 0, 320, 568);
tableView.frame = CGRectMake(0, 0, YFScreen.width, YFScreen.height);
tableView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:tableView];
_tableView = tableView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ - (UITableView *)tableView {

if (!_tableView) {
UITableView *tableView = [[UITableView alloc] init];
tableView.frame = CGRectMake(0, 0, 320, 568);
tableView.frame = CGRectMake(0, 0, YFScreen.width, YFScreen.height);
tableView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:tableView];
_tableView = tableView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ - (void)viewDidLoad {
[super viewDidLoad];

[self setupDataArr:@[@[@"MVVM_coderyi",@"YiTableViewController"],
@[@"DataSource分离",@"MyDataSourceViewController"],]];
@[@"DataSource分离",@"MyDataSourceViewController"],
@[@"MVP登录",@"MVPLoginViewController"]]];

}

Expand Down

0 comments on commit e163b25

Please sign in to comment.