赞
踩
代码实现:
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface MModel : NSObject
@property (nonatomic, strong) NSMutableDictionary* zhangHuDictionary;
- (void)dengLu;
- (void)zhuCe: (NSString*)zhangHao :(NSString*)miMa;
@end
NS_ASSUME_NONNULL_END
#import "MModel.h" @implementation MModel //重写初始化方法 - (MModel*) init { if (self = [super init]) { self.zhangHuDictionary = [[NSMutableDictionary alloc] init]; } return self; } - (void)dengLu { //通知方法,发送通知并携带字典将字典传给Controller [[NSNotificationCenter defaultCenter] postNotificationName:@"dengLu" object:self userInfo:self.zhangHuDictionary]; NSLog(@"登录时:%@", self.zhangHuDictionary); } - (void)zhuCe: (NSString*)zhangHao :(NSString*)miMa { [self.zhangHuDictionary setObject:miMa forKey:zhangHao]; //通知方法,发送通知给Controller完成注册反馈 [[NSNotificationCenter defaultCenter] postNotificationName:@"zhuCe" object:self userInfo:self.zhangHuDictionary]; NSLog(@"注册后:%@", self.zhangHuDictionary); } @end
#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface VView : UIView @property (nonatomic, strong) UITextField *zhangHaoTextField; @property (nonatomic, strong) UITextField *miMaTextField; @property (nonatomic, strong) UIButton *dengLuButton; @property (nonatomic, strong) UIButton *zhuCeButton; - (void)viewInit; @end NS_ASSUME_NONNULL_END
#import "VView.h" @implementation VView - (void)viewInit { self.backgroundColor = [UIColor whiteColor]; self.zhangHaoTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 200, 50)]; self.zhangHaoTextField.borderStyle = UITextBorderStyleRoundedRect; self.zhangHaoTextField.placeholder = @"账号"; [self addSubview:self.zhangHaoTextField]; self.miMaTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 350, 200, 50)]; self.miMaTextField.placeholder = @"密码"; self.miMaTextField.borderStyle = UITextBorderStyleRoundedRect; [self addSubview:self.miMaTextField]; self.dengLuButton = [UIButton buttonWithType:UIButtonTypeSystem]; [self.dengLuButton setTitle:@"登录" forState:UIControlStateNormal]; self.dengLuButton.frame = CGRectMake(100, 500, 90, 80); [self addSubview:self.dengLuButton]; self.zhuCeButton = [UIButton buttonWithType:UIButtonTypeSystem]; [self.zhuCeButton setTitle:@"注册" forState:UIControlStateNormal]; self.zhuCeButton.frame = CGRectMake(200, 500, 90, 80); [self addSubview:self.zhuCeButton]; } @end
VzhuCeView:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface VzhuCeView : UIView
@property (nonatomic, strong) UITextField *zhangHaoTextField;
@property (nonatomic, strong) UITextField *miMaTextField;
@property (nonatomic, strong) UIButton *zhuCeButton;
- (void)viewInit;
@end
NS_ASSUME_NONNULL_END
#import "VzhuCeView.h" @implementation VzhuCeView - (void)viewInit { self.backgroundColor = [UIColor whiteColor]; self.zhangHaoTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 200, 50)]; self.zhangHaoTextField.borderStyle = UITextBorderStyleRoundedRect; self.zhangHaoTextField.placeholder = @"账号"; [self addSubview:self.zhangHaoTextField]; self.miMaTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 350, 200, 50)]; self.miMaTextField.placeholder = @"密码"; self.miMaTextField.borderStyle = UITextBorderStyleRoundedRect; [self addSubview:self.miMaTextField]; self.zhuCeButton = [UIButton buttonWithType:UIButtonTypeSystem]; [self.zhuCeButton setTitle:@"注册" forState:UIControlStateNormal]; self.zhuCeButton.frame = CGRectMake(200, 500, 90, 80); [self addSubview:self.zhuCeButton]; } @end
#import <UIKit/UIKit.h> #import "MModel.h" #import "VView.h" #import "TiaoZhuanViewController.h" NS_ASSUME_NONNULL_BEGIN @interface ViewController : UIViewController //声明Model和View层的属性 @property (nonatomic, strong) MModel *mModel; @property (nonatomic, strong) VView *vView; @end NS_ASSUME_NONNULL_END
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建Model和View层的实例对象 self.vView = [[VView alloc] initWithFrame:self.view.bounds]; [self.vView viewInit]; //给View中的控件添加事件函数 [self.vView.zhuCeButton addTarget:self action:@selector(pressZhuCeTiaoZhuan) forControlEvents:UIControlEventTouchUpInside]; [self.vView.dengLuButton addTarget:self action:@selector(pressDengLu) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.vView]; self.mModel = [[MModel alloc] init]; //注册通知,接收从Model层传来的字典,当接收到通知时,执行事件函数 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dengLu:) name:@"dengLu" object:nil]; } //视图切换 - (void)pressZhuCeTiaoZhuan { TiaoZhuanViewController *tiaoZhuan = [[TiaoZhuanViewController alloc] init]; [self presentViewController:tiaoZhuan animated:YES completion:nil]; tiaoZhuan.mModel = self.mModel; } //登录按钮的事件函数是执行Model对象中的登录方法 - (void)pressDengLu { [self.mModel dengLu]; } //接收到通知后执行,登录时对账号密码进行判断 - (void)dengLu: (NSNotification*)notification { NSString *zhangHaoStr = [NSString stringWithFormat:@"%@",self.vView.zhangHaoTextField.text]; if ([self.vView.miMaTextField.text isEqual: notification.userInfo[zhangHaoStr]]) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登录成功" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil]; [self presentViewController:alert animated:YES completion:nil]; [alert addAction:action]; } else { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登录失败" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil]; [self presentViewController:alert animated:YES completion:nil]; [alert addAction:action]; } } @end
TiaoZhuanViewController:
#import <UIKit/UIKit.h>
#import "VzhuCeView.h"
#import "MModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface TiaoZhuanViewController : UIViewController
@property (nonatomic, strong) VzhuCeView *zhuCeView;
@property (nonatomic, strong) MModel *mModel;
@end
NS_ASSUME_NONNULL_END
#import "TiaoZhuanViewController.h" @interface TiaoZhuanViewController () @end @implementation TiaoZhuanViewController - (void)viewDidLoad { [super viewDidLoad]; self.zhuCeView = [[VzhuCeView alloc] initWithFrame:self.view.bounds]; [self.zhuCeView viewInit]; [self.zhuCeView.zhuCeButton addTarget:self action:@selector(pressZhuCe) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.zhuCeView]; //注册通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pressZhuCe:) name:@"zhuCe" object:nil]; } //点击按钮触发事件函数,调用Model对象的注册的方法,将用户输入的账号密码存入字典 - (void)pressZhuCe { [self.mModel zhuCe:self.zhuCeView.zhangHaoTextField.text :self.zhuCeView.miMaTextField.text]; } - (void)pressZhuCe: (NSNotification*)notification { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册成功" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil]; [self presentViewController:alert animated:YES completion:nil]; [alert addAction:action]; } @end
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。