将对象从控制器传递到视图
我正在关注斯坦福开放大学的 iPhone 开发课程 ,并且我在 taskment3 上被屏蔽了 2 天,也许有人可以帮助我这里?
任务是:
- 创建一个将显示 PolygonShape 对象的自定义 UIView 子类
- 为您的视图类提供对 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:
- Create a custom UIView subclass that will display your PolygonShape object
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在你弄清楚之后,补充一些 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
找到了我自己的答案,我错过了 CustomView 中的 setPolygon 方法来链接两者...愚蠢...
在 CustomView.h 中:
在 CustomView.m 中:
在 Controller.m 中>:
Found my own answer, I missed a setPolygon method in my CustomView to link both... stupid...
in CustomView.h:
in CustomView.m:
in Controller.m:
我昨晚刚刚完成作业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.
那么为什么不将它们声明为类的属性呢?
So why aren't you declaring them as properties of the class?