Reputation: 1
I have looked over the internet and seen people with similar issues, but not the exact same problem.
I am making a game for the iPhone that has an image of every letter (A-Z). At the start of the game I randomly assign a letter to 12 different UIImageViews
. I did this by creating an Array that holds all of the file names and then assigning a random one to each UIImageView
with rand()
.
When a user taps on a letter, I have it appear again in a different area where an answer word is being built. I then set the letter to NULL
so the user cannot use it again for their answer.
The question is, how can I distinguish between the images of letters when they get to the answer? So I can construct a string to compare it to a dictionary to see if it is an actual word.
I know that the file name gets lost, and each image is being assigned randomly. Is there a better way to handle this.
Upvotes: 0
Views: 90
Reputation: 306
You could use UIView instead of UIImageView. UIView has a property 'tag' (NSInteger) which you can set to identify your View later (see UIView tag)
I see talkol was a little bit too fast for me...
Upvotes: 0
Reputation: 12917
If I understand correctly, you want an easy way to find the letter back from an UIImageView.
Option 1 (better but more code):
Inherit from UIUmageView
and create your own class named something like LetterImageView
. Add a property to this class called letter
which holds an NSString
. When you construct this class, also set the letter property.
Instead of using the original UIImageView
, use your improved LetterImageView
. This will now remember which letter is which using your extra property.
Option 2 (faster but hackish):
Any view contains a property called tag
(this is an NSInteger
). You can set the tag to whatever value you want. It's supposed to help you label views, and then search for them according to their tag.
If you're not currently using the tag
property, you can save your letter there.. a bit ugly but will work. Simply assign a number for each letter, like 1 for A, 2 for B, etc. When you construct your UIImageView
, also save the letter in the tag. Whenever you want to discover which letter appears inside a specific image view, just examine the tag.
Upvotes: 1