I’m creating a Connect Four-type game in Xcode for a class project and am getting stuck on a helper function. I have a button above each column of the “game board” to drop the piece into the game. The board is essentially a 7×6 grid of UIImageView (whether or not this is the most practical I don’t really care about, it’s about as much as we have been taught in this class).
My helper function:
(declared in .h:
NSString* imgString;
UIImageView* prevImg;
UIImageView* curImg;
)
-(void) dropToken:(int) column
{
[ self checkTurn ];
for(int i=1; i<6; i++)
{
imgString = [ NSString stringWithFormat: @"img%d%d", i, column ];
curImg = (UIImageView*) imgString;
curImg.image = [ UIImage imageNamed: @"redpiece.png" ];
if(i != 6)
{
prevImg = curImg;
imgString = [ NSString stringWithFormat: @"img%d%d", i+1, column ];
curImg = (UIImageView*) imgString;
if(curImg.image == nil)
{
prevImg.image = nil;
curImg.image = curToken;
}
}
}
}
it’s supposed to take in the column as a parameter, and then go to the appropriate UIImageView, and set the image there to the current players token.
However I’m getting:
-[__NSCFString setImage:]: unrecognized selector sent to instance 0x6b09ee0
on the line: curImg.image = [ UIImage imageNamed: @”redpiece.png” ];
is there an issue with my typecast? Or am I overlooking some easily fixed issue?
Kevin has a good point in his comment, this line will never work:
I think you want to do something more like this:
This is code I’ve written in this page, so it might not even compile but the idea is you need to create the UIImageView with a UIImage with string you created.