在没有 nib 文件的 Objective-C 中,MVC 是否将逻辑与视图放在同一个类中?

发布于 2024-12-19 03:00:15 字数 181 浏览 4 评论 0原文

当我在 Objective-C 中以编程方式工作时,没有 nib 文件,并且逻辑在我的:

appViewController.m

在同一个类中该视图以及视图元素发生了什么?这违背了MVC模式吗?

我是否必须创建另一个班级并向两个班级发送消息?

When I work in Objective-C programatically with out nib files, and have the logic in my:

appViewController.m

having in the same class What is going on with that view, as well as with the View elements? Is this against the MVC pattern?

Do I have to create another class and message both classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉生梦死 2024-12-26 03:00:15

由你决定!如果您想分离层(M,V,C),您可以以编程方式创建自己的视图,并使用复合设计模式,在您的 UIView 子类中构建它,方法是从控制器中删除绘图代码。
也就是说...

  • 您创建一个“CustomCompositeView”,它
  • layoutSubview(继承自UIView)中 绘制所有 UI 元素
  • 扩展UIView将在 CustomViewController

,您将使用 loadView:代码显示您的视图:

- (void)loadView
{
CustomCompositeView *mainView = [[CustomCompositeView alloc] initWithFrame:aFrame];
[self setView:mainView];
[mainView release]; // remove this line if you are using ARC!
}

It's up to you! If you want to separate layers (M,V,C) you can create your own view programmatically and, by using composite design pattern, build it in your UIView subclass, by removing drawing code from your controller.
That is...

  • You create a "CustomCompositeView" that extends UIView
  • in layoutSubview (hinerited from UIView) you will draw all your UI elements
  • in your CustomViewController you will display your view using loadView:

code:

- (void)loadView
{
CustomCompositeView *mainView = [[CustomCompositeView alloc] initWithFrame:aFrame];
[self setView:mainView];
[mainView release]; // remove this line if you are using ARC!
}
落日海湾 2024-12-26 03:00:15

从技术上讲,它违背了MVC模式。你的 V 和 C 组合成一个对象。您可以将处理布局和绘图的代码分离到单独的 UIView 子类中。然后用loadView加载它:

// MyViewController.m

- (void)loadView {
    MyView* myView = [[[MyView alloc] init] autorelease];
    myView.delegate = self;
    self.view = myView;
}


#pragma mark - MyViewDelegate Methods

- (void)myViewSaveButtonWasPressed:(MyView *)myView {
    // do something
}

为了在视图和视图控制器之间进行通信,您可以定义委托协议。

// MyView.h

@class MyView;
@protocol MyViewDelegate <NSObject>
- (void)myViewSaveButtonWasPressed:(MyView *)myView;
@end

@class MyView : NSObject

@property (nonatomic, assign) id<MyViewDelegate> delegate;
// ...

当在视图中按下按钮(或类似的其他内容)时,会将其传递给委托。 ViewController 应符合委托方法并以这种方式处理实际逻辑本身。

Technically, it is going against the MVC pattern. Your V and C and combined into a single object. You can seperate the code that handles layout and drawing into a seperate UIView subclass. Then load it with loadView:

// MyViewController.m

- (void)loadView {
    MyView* myView = [[[MyView alloc] init] autorelease];
    myView.delegate = self;
    self.view = myView;
}


#pragma mark - MyViewDelegate Methods

- (void)myViewSaveButtonWasPressed:(MyView *)myView {
    // do something
}

To communicate between the view and the view controller, you can define a delegate protocol.

// MyView.h

@class MyView;
@protocol MyViewDelegate <NSObject>
- (void)myViewSaveButtonWasPressed:(MyView *)myView;
@end

@class MyView : NSObject

@property (nonatomic, assign) id<MyViewDelegate> delegate;
// ...

When a button is pressed in the view (or something else along those lines) pass that on to the delegate. The ViewController should conform to the delegate method and handle the actual logic itself that way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文