Objective-C,使用协议访问数据源

发布于 2024-12-04 10:53:01 字数 1385 浏览 0 评论 0原文

我正在使用 MVC 模型,但无法获取视图所需的数据,我正在尝试使用数据源,因为视图永远不应该拥有其数据。通常,协议用于创建数据源。

  • 我有一个 MVC:CalculatorBrain.[hm] - CalculatorViewController.[hm] - (视图是 .xib)
  • 我也有 MVC:GraphingView - GraphingViewController - (模型是我无法获取的数据)

目标是:当我按下计算器上的一个按钮,它会绘制当前显示的函数(例如:x+5=)。计算器部分负责表达式/函数、显示等,而绘图部分则负责绘制。 CalculatorViewController 应该是 GraphingView 的源,但数据始终保持为空。

这是带有协议声明的 GraphingView.h:

@class GraphingView;

@protocol GraphingViewSource
- (float)getDataPoints:(GraphingView *)requestor;
@end

@interface GraphingView : UIView {
    id <GraphingViewSource> source;
}

@property (assign) id <GraphingViewSource> source;

@end

CalculatorViewController.m 通过实现 getDataPoints: 方法来实现协议。 CalculationViewController.h 的标头:

@interface CalculatorViewController : UIViewController <GraphingViewSource>

按下应设置数据源的按钮:

- (IBAction)graphPressed:(UIButton *)sender
{
    GraphingViewController *graphingVC = [[GraphingViewController alloc] init];
    graphingVC.graphingView.source = self;
    [self.navigationController pushViewController:graphingVC animated:YES];
    [graphingVC release];
}

按下按钮会很好地显示新视图等,但以下代码行仅返回 null(在 GraphingView.m 内):

float result = [self.source getDataPoints:self];

看来,我无法访问我的数据来源...

I'm using the MVC model but I cannot get the data needed for the View, I am trying to use a data source since a View should never own its data. Typically, a protocol is used to create a data source.

  • I have a MVC: CalculatorBrain.[hm] - CalculatorViewController.[hm] - (view is .xib)
  • I also have MVC: GraphingView - GraphingViewController - (model is the data I can't get)

The goal is: when I press a button on the calculator, it draws the function (e.g.: x+5=) that is currently in it's display. The calculator part takes care of the expression/function, the display, etc while the graphing part should draw. CalculatorViewController should be the GraphingView's source, but the data always stays null.

This is GraphingView.h with the declaration of the protocol :

@class GraphingView;

@protocol GraphingViewSource
- (float)getDataPoints:(GraphingView *)requestor;
@end

@interface GraphingView : UIView {
    id <GraphingViewSource> source;
}

@property (assign) id <GraphingViewSource> source;

@end

CalculatorViewController.m implements the protocol by implementing the getDataPoints: method. Header of CalculationViewController.h :

@interface CalculatorViewController : UIViewController <GraphingViewSource>

Pressing the button which should set up the data source :

- (IBAction)graphPressed:(UIButton *)sender
{
    GraphingViewController *graphingVC = [[GraphingViewController alloc] init];
    graphingVC.graphingView.source = self;
    [self.navigationController pushViewController:graphingVC animated:YES];
    [graphingVC release];
}

Pressing the button brings up the new view nicely etc, but following line of code only returns null (inside GraphingView.m) :

float result = [self.source getDataPoints:self];

It seems, I cannot access my data source...

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

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

发布评论

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

评论(1

美胚控场 2024-12-11 10:53:01

从您的评论来看,在您尝试设置其源时, graphingView 似乎是 nil

如果视图控制器有一个 xib,你应该这样做:

GraphingViewController *graphingVC = [[GraphingViewController alloc] initWithNibName:@"GraphingViewController" bundle:nil];

而不是一个普通的 init

这将创建并实例化所有视图对象,以便在呈现视图控制器之前可以设置它们的属性。

From your comments it seems that graphingView is nil at the time you are trying to set its source.

If there is a xib for the view controller, you should be doing this:

GraphingViewController *graphingVC = [[GraphingViewController alloc] initWithNibName:@"GraphingViewController" bundle:nil];

Instead of a plain init.

This will create and instantiate all of your view objects so they are available to have properties set before you present the view controller.

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