gpt4 book ai didi

iOS - 如何将信息从 VIEW 传递到 Controller?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:48:18 25 4
gpt4 key购买 nike

总的来说,我对编程还比较陌生,一直在关注 iTunesU 上的 CS193p 视频。我目前正在做作业 3,并且无法从发送到 View Controller 的 View 中获取一些信息。

我相信我已经正确地设置了整个委托(delegate),所以真正的问题是如何让我的 View Controller 看到一些信息(比如 self.bounds.size.width ),这是只有 View 才有的属性。这会涉及使用 self.dataSource 吗?如果是这样,我可以通过什么方式传递这些信息?我的最终目标是让 View Controller 对 View 的属性执行一些转换,并将其发送回 View 的 drawRect 以便可以绘制它。

谢谢!!

** 根据要求进行编辑,我在下面发布了部分 drawRect 代码

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat scale = 32;

CGPoint midPoint;
midPoint.x = self.bounds.origin.x + self.bounds.size.width/2;
midPoint.y = self.bounds.origin.y + self.bounds.size.height/2;

// ---- finding the y starting point

float totalXsInWidth;
totalXsInWidth = self.bounds.size.width / scale;

float leftMostX = totalXsInWidth / -2;
float graphResultY = sin(leftMostX); // ** in theory, I want "leftMostX" to be modifed by the equation entered (in the CONTROLLER)
NSLog(@"The leftMostX is %f", leftMostX);

[self.dataSource passingVariable:leftMostX]; //** Here I pass the variable from drawRect to get modifyed in the CONTROLLER

float graphResultY1;

graphResultY1 = 5; //this is a test, I want to see if the controller actually effect a change
graphResultY1 = [self.dataSource calcResult:self]; //this should now be a different number than 5 or 0 (the init value)

NSLog(@"From graphingview, the result is %f", graphResultY1); //** unfortunately = 0... :(

最佳答案

阅读了 CS193p 作业 3(for anyone interested, it is available here in PDF format)后,您似乎被要求为您的 UIView 子类创建一个协议(protocol),并让该协议(protocol)的委托(delegate)( View 的管理 View Controller )提供数据用于绘图。

如果您正确设置了协议(protocol), View 的drawRect 方法应该通过协议(protocol)的方法向 View Controller 请求数据,类似于:

DataObject *data = [self.delegate getData];
// It might be [self.dataSource getData]; in your case

那应该调用你应该写入 View Controller 的 getData 委托(delegate)方法(我为此编写了一个方法签名,以适应你的协议(protocol)使用的那个。另外,这段代码不考虑相关的内存管理,如果需要的话):

- (DataObject *)getData
{
// Get data from the model, and return
DataObject *dataObjectToReturn = [Model getRelevantData];

return dataObjectToReturn;
}

View 的 drawRect 现在应该具有 DataObject 的相关实例,并且可以着手使用该数据来绘制它需要绘制的内容。


以下是我的原始答案,与上面的具体问题无关,但确实显示了另一种 View / View Controller 交互的方法。 (此方法不适用于上述问题,因为绘图所需的数据不应该由 View 本身拥有。)

要从 View 的 Controller 访问 View 的属性,您可以使用 self.view。因此,要获得 View 的宽度,就像在您的示例中一样,您可以使用:

CGFloat viewWidth = self.view.bounds.size.width;

您还可以设置 Controller 的 View ,但请注意,对于您的示例,您必须为框架提供整个 CGRect,因为不允许直接操作宽度:

// This gets a reference to the view's frame, then uses values from it to create a new
// CGRect that is 10.0 points wider, and sets the frame of the view to the new frame

CGRect viewFrame = self.view.frame;
self.view.frame = CGRectMake(viewFrame.origin.x, viewFrame.origin.y, viewFrame.size.width + 10.0f, viewFrame.size.height);

关于iOS - 如何将信息从 VIEW 传递到 Controller?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12730570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com