设计模式 - Objective-C - MVC 模型视图控制器 [英] Design Pattern - Objective-C - MVC Model View Controller

查看:23
本文介绍了设计模式 - Objective-C - MVC 模型视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经阅读了有关 MVC 的网络教程,并且已经阅读了此处的主题.我想我明白了 MVC 的概念,但我不确定它的实现.

Hi I already read tutorials around the web on MVC and already read the topics on here. I think i got the concept of the MVC but i'm not sure of its implementation.

我尝试将它应用到一个简单的程序,一个带有标签和按钮的窗口.按钮增加一个计数器,标签显示它的值.

I've tried to apply it to a simple program, a window that have a label and a button. The button increase a counter and the label shows the value of it.

我尝试了两种不同的方式.

I tried in 2 different ways.

在第一种情况下(示例有效)我融化了视图和控制器.正如我所说,该示例有效,但我希望你们告诉我它是否是 MVC 的正确实现,或者它没有遵循正确的设计.

In the first case ( the example works ) i melt the View and the Controller. As i said, the example works, but i want you guys to tell me if it's a correct implementation for MVC or it's not following the right design.

第二个示例将模型视图和控制器作为 3 个独立的类,但该示例不起作用,因为 V 和 C 本身是导入的,所以我希望你们告诉我我做错了什么.

The second example has Model View and Controller as 3 separated class, but the example doesnt work because the V and the C import itself, so i would love you guys to tell me where i'm doing wrong.

第一个版本:模型,视图控制器

//Model.h
#import <Foundation/Foundation.h>

@interface Model : NSObject {
    int _counter;
}

-(void)setCounter:(int)valueCounter;
-(int)getCounter;
-(void)increaseCounter;
@end

//Model.m
#import "Model.h"
@implementation Model {}

-(void)setCounter:(int)valueCounter { _counter = valueCounter; }
-(int)getCounter { return _counter; }
-(void)increaseCounter{ _counter ++; }
@end


//ViewController.h
#import <UIKit/UIKit.h>
#import "Model.h"

@interface ViewController : UIViewController {
    IBOutlet UIButton *_button;
    IBOutlet UILabel *_label;
    Model *myModel;
}

-(IBAction)send:(id)sender;
@end

//ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
     myModel = [[Model alloc]init];
    _label.text = [NSString stringWithFormat:@"%d",[myModel getCounter]];
}

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

- (IBAction)send:(id)sender{
    [myModel increaseCounter];
    _label.text = [NSString stringWithFormat:@"%d",[myModel getCounter]];
}

@end



这种方式是 MVC 的正确模式吗?代码有效,但在我开始更复杂的应用程序之前,我想确保我以一种好的方式对其进行编码.这就是我将如何做这个应用程序,我的 MVC 方式.是不是很糟糕?好的?如何更改或修复它?

Is this way a correct Pattern for MVC ? The code works, but before i start more complex apps i want to make sure i code it in a good way. This is how i would do this app, my way of MVC. is it bad? good? how to change or fix it?







第二个版本:模型、视图、控制器分离

----> 这是模型

//Model.h
#import <Foundation/Foundation.h>

@interface Model : NSObject {
    int _count;
}

-(void)setCount:(int)value;
-(int)getCount;
-(void)increaseCount;

@end

//Model.m
#import "Model.h"

@implementation Model

-(void)setCount:(int)value { _count = value; }
-(int)getCount { return _count; }
-(void)increaseCount { _count = _count++; }

@end

----> 这是视图

//View.h
#import <UIKit/UIKit.h>
#import "Controller.h"

@interface ViewController : UIViewController{
    IBOutlet UILabel *label;
    IBOutlet UIButton *button;
    Controller *myController;
}

@end

//View.m
#import "ViewController.h"
#import "Controller.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    myController = [[Controller alloc]init];
}

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


-(IBAction)pressButton:(id)sender{
    label.text = [NSString stringWithFormat:@"%d",[myController actionIncrease]];
}

@end

----> 这是控制器

----> This is the Controller

//Controller.m
#import <Foundation/Foundation.h>

@class "Model.h"
@class  "ViewController.h"

@interface Controller : NSObject {
    Model *_mymodel;
    UIViewController *_myviewController;
}

-(int)actionIncrease;

@end

//Controller.m
#import "Controller.h"
#import "Model.h"

@implementation Controller

-(id)init{
    _mymodel = [[Model alloc]init];
}

-(int)actionIncrease {
    [_mymodel increaseCount];
    return [_mymodel getCount];    
}

@end



这个版本不起作用,因为类视图和控制器相互导入,编译器给了我一个警告



this version doesn't work because the classes view and controller import each other and the compiler gives me a warning

推荐答案

简单地说:UIViewController 不是你的视图,而是你的控制器

Simply: UIViewController is not your view, it's your controller

  • UIViewController 控制 UIView 上显示的内容
  • UIView 的主要目的是包含子视图.
  • NSObject 可以被任何类使用,但应该被 UIViewController 使用.
  • UIViewController controls WHAT is displayed on the UIView
  • UIView's main purpose is to contain subviews.
  • NSObject can be used by any class, but should be used by the UIViewController.

诚然,在完成 codeschool 的教程后,我对它的理解要好得多http://www.codeschool.com/courses/try-ios.我强烈推荐这种简单的实践方法.

Admittedly, I understood it much better after completing codeschool's tutorial http://www.codeschool.com/courses/try-ios. I highly recommend this simple hands-on approach.

让我们分解一下:

注意:这里我们使用 @property 声明代替.这些将使您免于编写自己的 setter 和 getter 方法.(除非您需要为自定义功能覆盖它们)

Note: Here we utilize @property declarations instead. These will save you from writing your own setter and getter methods. (unless you need to override them for custom functionality)

//MyModelObject.h
#import <Foundation/Foundation.h>

@interface MyModelObject : NSObject

@property (nonatomic) int count; 

@end

UIView(视图):

//MyView.h
#import <UIKit/UIKit.h>

@interface MyView : UIView

// holds it's own subviews
@property (strong, nonatomic) UIView *anotherView;
@property (strong, nonatomic) UIImageView *myImageView;

@end

UIViewController(控制器,一切都在这里!):

//MyViewController.h
#import <Foundation/Foundation.h>

#import "MyView.h"  // your custom view
#import "MyModel.h" // your custom model

@interface MyViewController : UIViewController

@property (strong, nonatomic) MyView *myView 
// see how the view is "owned" by the view controller?

@end



//MyViewController.m

@implementation MyViewController 

@synthesize myView;


- (void) someMethod {

    [myView doSomething]; 

}

@end

这篇关于设计模式 - Objective-C - MVC 模型视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆