Objective C、iOS,如何将几个 int 变量附加到属于 IBOutletCollection 一部分的 UIImageView?
我需要跟踪一些可以在屏幕上拖动并位于其他图像视图中的图像视图。例如足球进球。我想通过为足球、当前进球和进球中看到的时间附加一些额外的属性来做到这一点。
@property (nonatomic, retain) IBOutlet IBOutletCollection(UIImageView)
NSArray *multipleFootballs;
我想,我必须创建一个超级类。 虽然我不确定最好的方法是什么?
EDIT3:谢谢尼克,但是我如何访问实例属性?
@interface FootballImageView : UIImageView {
int intCurrentGoal;
}
@property (readwrite) int intCurrentGoal;
@implementation FootballImageView
@synthesize intCurrentGoal;
-(id)init {
self = [super init];
if(self) {
// do your initialization here...
}
return self;
}
@end
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (id football in multipleFootballs) {
//if ([[touches anyObject] intCurrentGoal] == 0) {
//if (football.intCurrentGoal == 0) {
I need to keep track of some image views which can be dragged around the screen and located within other images views. For example footballs in goals. I'd like to do this by having some extra properties attached to the footballs, current goal and times seen in the goal.
@property (nonatomic, retain) IBOutlet IBOutletCollection(UIImageView)
NSArray *multipleFootballs;
I figure, that I have to create a super class.
Although I'm not sure the best way to do this ?
EDIT3 : Thanks nick, but how do I then access a instance property ?
@interface FootballImageView : UIImageView {
int intCurrentGoal;
}
@property (readwrite) int intCurrentGoal;
@implementation FootballImageView
@synthesize intCurrentGoal;
-(id)init {
self = [super init];
if(self) {
// do your initialization here...
}
return self;
}
@end
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (id football in multipleFootballs) {
//if ([[touches anyObject] intCurrentGoal] == 0) {
//if (football.intCurrentGoal == 0) {
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该很简单:
IBOutletCollection(UIImageView) )
到IBOutletCollection(MyImageView)
-
回复编辑 3
您面临的问题是您在 TouchesBegan 中使用的匿名类型 (id)。放入这样的支票:
Should be easy:
IBOutletCollection(UIImageView)
toIBOutletCollection(MyImageView)
-
Reply for edit 3
The problem you are facing is the anonymous type (id) you are using in touchesBegan. Put in a check like this:
通常(也是好的)建议是您应该避免在视图对象中保留状态。目标中看到的时间和当前目标等属性应该是数据模型的一部分,而不是附加到视图中。这样做有很多充分的理由,但它们大多归结为两大点:
作为第一点的示例,请考虑当用户从主游戏屏幕切换到更改设置、接听电话等时会发生什么。在我看来,您有三种选择:a)保留所有视图,即使他们没有显示您可以维护应用程序的状态; b) 在释放视图之前保存视图的状态; c) 防止用户退出游戏。 (a) 和 (c) 看起来都不是好主意,如果您采用 MVC 方法,您就可以免费获得 (b)。
相反,创建一个可以代表游戏所有方面的数据模型。您可以使用标准类(如 NSDictionary 和 NSSet),或您自己设计的类(如 Football 和 Pitch),或者(最有可能)组合。然后创建一个游戏控制器,观察相关模型对象的变化并在相应的视图中进行更改。
The usual (and good) advice is that you should avoid keeping state in view objects. Attributes like times seen in goal and current goal should be part of your data model, not attached to your views. There are lots of good reasons for this, but they mostly boil down to two big points:
As an example of the first point, consider what happens when the user switches away from the main game screen to change settings, answer a phone call, etc. As I see it, you have three choices: a) preserve all the views even though they're not being displayed that you can maintain the app's state; b) save the state from the views before releasing the views; c) prevent the user from switching away from the game. Neither (a) nor (c) seem like good ideas, and if you take the MVC approach you get (b) for free.
Instead, create a data model that can represent all the aspects of your game. You can use standard classes like NSDictionary and NSSet, or classes of your own design like Football and Pitch, or (most likely) a combination. Then create a game controller which observes changes to the relevant model objects and makes changes in the corresponding views.