将对象从控制器传递到视图

发布于 2024-07-09 22:27:03 字数 1213 浏览 5 评论 0原文

我正在关注斯坦福开放大学的 iPhone 开发课程 ,并且我在 taskment3 上被屏蔽了 2 天,也许有人可以帮助我这里?

任务是:

  1. 创建一个将显示 PolygonShape 对象的自定义 UIView 子类
  2. 为您的视图类提供对 PolygonShape 对象的访问权限,以便它可以根据需要检索多边形的详细信息

问题是: 我如何为我的视图类提供访问权限访问我的控制器中定义的多边形对象?

这是我的实现(如果有帮助的话):

CustomView.h:

#import "PolygonShape.h"

@interface CustomView : UIView {
    IBOutlet PolygonShape *polygon;
}
- (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides;

@end

Controller.h:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "PolygonShape.h"
#import "PolygonView.h"

@interface Controller : NSObject {
    IBOutlet UIButton *decreaseButton;
    IBOutlet UIButton *increaseButton;
    IBOutlet UILabel *numberOfSidesLabel;
    IBOutlet PolygonShape *polygon;
    IBOutlet PolygonView *polygonView;
}
- (IBAction)decrease;
- (IBAction)increase;
- (void)awakeFromNib;
- (void)updateInterface;
@end

I'm following iPhone dev courses from Stanford Open-University, and I've been blocked for 2 days on assignment3, maybe someone can help me here?

The tasks are:

  1. Create a custom UIView subclass that will display your PolygonShape object
  2. Give your view class access to the PolygonShape object so that it can retrieve the details of the polygon as needed

The problem is: how do I give my view class access to the polygon object defined in my controller?

Here is my implementations if it can help:

CustomView.h:

#import "PolygonShape.h"

@interface CustomView : UIView {
    IBOutlet PolygonShape *polygon;
}
- (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides;

@end

Controller.h:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "PolygonShape.h"
#import "PolygonView.h"

@interface Controller : NSObject {
    IBOutlet UIButton *decreaseButton;
    IBOutlet UIButton *increaseButton;
    IBOutlet UILabel *numberOfSidesLabel;
    IBOutlet PolygonShape *polygon;
    IBOutlet PolygonView *polygonView;
}
- (IBAction)decrease;
- (IBAction)increase;
- (void)awakeFromNib;
- (void)updateInterface;
@end

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

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

发布评论

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

评论(4

゛时过境迁 2024-07-16 22:27:03

在你弄清楚之后,补充一些 Objective-C 基础知识可能不会有什么坏处:

http://www.cocoacast.com/?q=node/103

And after you figure it out, it might not hurt to touch up on some objective-c basics:

http://www.cocoacast.com/?q=node/103

我的奇迹 2024-07-16 22:27:03

找到了我自己的答案,我错过了 CustomView 中的 setPolygon 方法来链接两者..​​.愚蠢...

CustomView.h 中:

#import "PolygonShape.h"

@interface CustomView : UIView {
    IBOutlet PolygonShape *polygon;
}

@property (readwrite, assign) PolygonShape *polygon;

- (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides;

@end

在 CustomView.m 中:

@implementation CustomView

@synthesize polygon;

...

@end

Controller.m 中>:

- (void)awakeFromNib { 
    // configure your polygon here 
    polygon = [[PolygonShape alloc] initWithNumberOfSides:numberOfSidesLabel.text.integerValue minimumNumberOfSides:3 maximumNumberOfSides:12];
    [polygonView setPolygon:polygon];
    NSLog (@"My polygon:  %@", [polygon description]);
} 

Found my own answer, I missed a setPolygon method in my CustomView to link both... stupid...

in CustomView.h:

#import "PolygonShape.h"

@interface CustomView : UIView {
    IBOutlet PolygonShape *polygon;
}

@property (readwrite, assign) PolygonShape *polygon;

- (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides;

@end

in CustomView.m:

@implementation CustomView

@synthesize polygon;

...

@end

in Controller.m:

- (void)awakeFromNib { 
    // configure your polygon here 
    polygon = [[PolygonShape alloc] initWithNumberOfSides:numberOfSidesLabel.text.integerValue minimumNumberOfSides:3 maximumNumberOfSides:12];
    [polygonView setPolygon:polygon];
    NSLog (@"My polygon:  %@", [polygon description]);
} 
鲜肉鲜肉永远不皱 2024-07-16 22:27:03

我昨晚刚刚完成作业3。 我在 Interface Builder 中解决了这个连接。 首先,我在 PolygonShape 的“PolygonView”UIView 子类上创建了一个出口,然后将其连接到 Polygon 模型的实例。 根据我在 Google Group 和其他各个网站上读到的内容,我认为没有一种正确的方法可以将此 UIView 连接到模型和控制器。 但它有效,我认为视图了解模型没有任何问题。

I just finished assignement 3 last night. I solved this connection all in Interface Builder. First I created an outlet on the "PolygonView" UIView subclass for the PolygonShape and then connected it to the instance of the Polygon model. From what I have read in the Google Group and on various other sites, I do not think there is one right way to connect this UIView to the model and the controller. But it worked I think there is nothing wrong with the View knowing about the model.

回梦 2024-07-16 22:27:03

那么为什么不将它们声明为类的属性呢?

So why aren't you declaring them as properties of the class?

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