
35101_541943_663134.jpg
大概因為在編程方面沒有天份吧,對MVC一直不是很了解.直到今天看到一篇博文.內(nèi)容雖然很少,但是對于MVC初學(xué)者真的很棒!(原文地址: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上的文字顏色設(shè)置為黑色,以便顯示
[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模式發(fā)送一個通知,用于通知controller要做什么事情
*userInfo:這里增添name鍵值,是為了在控制器區(qū)分通知
*/
[[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];
/**
*當(dāng)這個controller接收到一個名稱為@"successful"通知后,執(zhí)行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)]; //初始化時一定要設(shè)置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]; //調(diào)用MModel.h中的方法(API)
_nlView.successfulLabel.text = @"保存成功";
}
- (void)loadBtnPressed : (UIButton*)sender{
[_nlModel load]; //調(diào)用MModel.h中的方法(API)
}
- (void)saveOK : (NSNotification*) notification{
NSString *str;
/**
*根據(jù)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
這就是所有工程文件,在學(xué)習(xí)MVC的同時,也看了通知的知識.