views:

201

answers:

1

I am trying to create a card game with a standard deck of cards. I need to be able to create a new card on screen every time someone draws a card from the deck and then be able to continuously move it throughout gameplay. I have image files for every card available in the game.

I have tried creating a card object which held a UIImageView for the card along with some other basic data but I had trouble referring back to that card to move it again after the first touch. Was this the right way to do this?

+1  A: 

If I understand your question correctly, you're having trouble finding which "card" is associated with which "view" sometime later, when using some API that passes you the view object. I think the answer, if this is the case, is that you have your dependencies upside down. When using Model-View-Controller, which Cocoa is big on, you should create a UIImageView subclass that contains a reference to your Card object. I would make a protocol (CardDelegate?) and implement the protocol with your card object (getValue, getSuit, etc...) Your custom view would have a method e.g. getCardDelegate that returns the delegate from your custom view. The delegate holds all the state for your card. Later, when you get the view from some API, you can send messages to its delegate to change the state of your program, because the view refers to the model. You'll notice this is the same pattern that Interface Builder uses: you build a view, and then link it to some delegate that does all the work.

Matt