PDA

View Full Version : How to create a custom property and identifier?




mandude
Aug 11, 2011, 05:03 PM
hey all I'm sure this is a very easy thing to do and it's done frequently but I'm wondering how to make a custom property for a UIImageView be set to a created identifier. This works great with a UIIamge, of course, however what if i want to assign the image view a custom property, suit for example. Im making a simple card game and I'd like to assign a card's ImageView a 'suit' property with an identifier, such as clubs, hearts, spades, or diamonds an example of what I'm looking for would be:

.h

UIImageView *card
UIImage *image;

@property (nonatomic, retain) UIImageView *card;


.m


card.image = image;
card.suit = clubs;
//or
card.suit = diamonds;


Then of course the identifiers need to be able to be compared between two card for the game I'm making, say if two cards are of the same suit, they interact and an event occurs because of it...


if (card1.suit == card2.suit) {
//magical things happen
}


is this sort of stuff possible? I'm sure it is, some guidance on what to put in the header file would be great thanks.



ViviUO
Aug 11, 2011, 10:46 PM
I believe you will have to make your own subclass of UIImageView to manipulate it the way you want to.

North Bronson
Aug 12, 2011, 01:37 PM
You could try to put the different suits in an enumerated data type:

typedef enum
{
NBSCardViewStyleNone,
NBSCardViewStyleAce,
NBSCardViewStyleClub,
NBSCardViewStyleHeart,
NBSCardViewStyleSpade
}
NBSCardViewStyle;

and then create your card views with something like:

- (id)initWithFrame:(CGRect)frame style:(NBSCardViewStyle)style
{
self = [self initWithFrame: frame];

if (self)
{
switch (style)
{
case NBSCardViewStyleAce:
{
break;
}
case NBSCardViewStyleClub:
{
break;
}
case NBSCardViewStyleHeart:
{
break;
}
case NBSCardViewStyleSpade:
{
break;
}
default:
{
break;
}
}
}

return self;
}

but you probably want to not keep too much data in your view classes. Is there a way to keep the data in your model classes and try to run the application from that?