关于MVC以及通知

2017-03-08  本文已影响69人  心里的另一个你
35101_541943_663134.jpg
大概因为在编程方面没有天份吧,对MVC一直不是很了解.直到今天看到一篇博文.内容虽然很少,但是对于MVC初学者真的很棒!(原文地址:http://blog.csdn.net/u012361288/article/details/51701676)
下面是针对博文作者写的demo做的其他方面的一些修改.写此文,只是为了梳理个人思路.
首先是View文件
NLView.h
#import <UIKit/UIKit.h>

@interface NLView : UIView
/** send按钮 */
@property (nonatomic, strong)  UIButton* sendBtn;
/** receive按钮 */
@property (nonatomic, strong)  UIButton* receiveBtn;
/** 保存成功label */
@property (nonatomic, strong)  UILabel* successfulLabel;
/**
   *初始化控件
   */
- (void)viewInit;
@end

NLView.m

#import "NLView.h"

@implementation NLView
- (void)viewInit
{
         _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
         
         [_sendBtn setFrame:CGRectMake(50, 50, 150, 80)];
         
         [_sendBtn setTitle:@"保存" forState:UIControlStateNormal];
         
         [_sendBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  //默认的页面背景色是白色,butotn上的文字的默认颜色也是白色,所以在此处将button上的文字颜色设置为黑色,以便显示
         
         [self addSubview:_sendBtn];
         
         
         _receiveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
         
         [_receiveBtn setFrame:CGRectMake(50, 160, 150, 80)];
         
         [_receiveBtn setTitle:@"加载" forState:UIControlStateNormal];
         
         [_receiveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
         
         [self addSubview:_receiveBtn];
         
         _successfulLabel = [[UILabel alloc] init];
         
         [_successfulLabel setFrame:CGRectMake(50, 240, 150, 80)];
         
         [_successfulLabel setTextColor:[UIColor blackColor]];
         
         [self addSubview:_successfulLabel];
}

@end

然后是Model
NLModel.h

#import <Foundation/Foundation.h>

@interface NLModel : NSObject
- (void)save;
- (void)load;
@end

NLModel.m

#import "NLModel.h"

@implementation NLModel
- (void)save
{
         NSLog(@"**************保存************");
         /**
            *使用Notification模式发送一个通知,用于通知controller要做什么事情
            *userInfo:这里增添name键值,是为了在控制器区分通知
            */
         [[NSNotificationCenter defaultCenter] postNotificationName:@"successful" object:self userInfo:@{@"name":@"successful"}];
}
- (void)load
{
         NSLog(@"*************加载************");
         [[NSNotificationCenter defaultCenter] postNotificationName:@"load" object:self userInfo:@{@"name":@"load"}];
}
@end

最后是Controller奉上
ViewController.h

#import <UIKit/UIKit.h>
#import "NLView.h"
#import "NLModel.h"

@interface ViewController : UIViewController
/** 实例化一个NLView对象 */
@property (nonatomic, strong)  NLView* nlView;
/** 实例化一个NLModel对象 */
@property (nonatomic, strong)  NLModel* nlModel;
@end

ViewController.m

#import "ViewController.h"
#define deviceScreenWidth [[UIScreen mainScreen]bounds].size.width

#define deviceScreenHeight [[UIScreen mainScreen]bounds].size.height
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
         [super viewDidLoad];
         /**
            *当这个controller接收到一个名称为@"successful"通知后,执行saveOk:
            */
         [[NSNotificationCenter defaultCenter] addObserver:self
                                                                               selector:@selector(saveOK:)
                                                                                   name:@"successful"
                                                                                  object:nil];
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveOK:) name:@"load" object:nil];
         _nlView = [[NLView alloc]initWithFrame:CGRectMake(0, 0, deviceScreenWidth, deviceScreenHeight)];  //初始化时一定要设置frame,否则VView上的两个按钮将无法被点击
         
         [_nlView viewInit];
         
         [_nlView.sendBtn addTarget:self action:@selector(saveBtnPressed:) forControlEvents:UIControlEventTouchUpInside];  //为“保存”按钮添加target-action模式
         
         [_nlView.receiveBtn addTarget:self action:@selector(loadBtnPressed:) forControlEvents:UIControlEventTouchUpInside];  //为“加载”按钮添加target-action模式
         
         [self.view addSubview:_nlView];
         
         _nlModel = [[NLModel alloc]init];
         
}

- (void)saveBtnPressed : (UIButton*)sender{
         
         [_nlModel save];  //调用MModel.h中的方法(API)
         _nlView.successfulLabel.text = @"保存成功";
}

- (void)loadBtnPressed : (UIButton*)sender{
         
         [_nlModel load];  //调用MModel.h中的方法(API)
}


- (void)saveOK : (NSNotification*) notification{
         
         NSString *str;
         /**
            *根据userInfo中的name判断,是那个通知
            */
         if([notification.userInfo[@"name"] isEqualToString:@"load"])
         {
            str = @"加载中";
         }
         else if([notification.userInfo[@"name"] isEqualToString:@"successful"])
         {
            str= @"保存成功";
         }
         UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"恭喜"
                                                                                  message:str
                                                                           preferredStyle:UIAlertControllerStyleAlert];
         
         UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                  
                  NSLog(@"点击了取消按钮");
                  
         }];
         
         UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                  
                  NSLog(@"点击了确定按钮");
                  
         }];
         
         [alertController addAction:cancelAction];
         [alertController addAction:okAction];
         
         [self presentViewController:alertController animated:YES completion:nil];  
         
         
}
- (void)didReceiveMemoryWarning {
         [super didReceiveMemoryWarning];
         // Dispose of any resources that can be recreated.
}


@end

这就是所有工程文件,在学习MVC的同时,也看了通知的知识.

上一篇下一篇

猜你喜欢

热点阅读