开发者

Objective C, iOS, how do I attach a couple of int variables to UIImageView which is part of an IBOutletCollection?

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;开发者_JAVA技巧 
}

@end

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (id football in multipleFootballs) {

    //if ([[touches anyObject] intCurrentGoal] == 0) {
    //if (football.intCurrentGoal == 0) {


Should be easy:

  • Create a new subclass inheriting from UIImageView, let's call it MyImageView
  • Add your custom instance variables you need to the header
  • Choose as class for the old UIImageViews the new MyImageView in interface builder (Identity Tab)
  • Change IBOutletCollection(UIImageView) to IBOutletCollection(MyImageView)

-

- (id)init 
{
    self = [super init];
    if(self) {
        // do your initialization here...
    }
    return self; 
}

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:

for (FootballImageView *football in multipleFootballs) {
    if(football.intCurrentGoal == 0) {
        football.intCurrentGoal++;
    } 
}


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:

  1. Mixing state with views will make things unnecessarily complicated.
  2. Cocoa Touch is designed with an expectation that apps will adopt the MVC paradigm.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜