0

I am trying to set a UIImageView's image equal to a certain image:

- (IBAction)showCard:(id)sender {
    for (KCCardView *cardView in self.view.subviews) {
        for (KCCard *aCard in dealerHand) {
            cardView.image = aCard.cardImage;
        }
    }
}

But all that keeps happening is the app crashes and the following is logged to the console. I can't seem to find the bug.

-[UIRoundedRectButton setImage:]: unrecognized selector sent to instance

Thank you!

    (gdb) backtrace
#0  0x012deccc in objc_exception_throw ()
#1  0x01150f0d in -[NSObject doesNotRecognizeSelector:] ()
#2  0x010b5e2f in ___forwarding___ ()
#3  0x010b5c12 in __forwarding_prep_0___ ()
#4  0x011510e9 in -[NSObject performSelector:withObject:withObject:] ()
#5  0x000197fd in -[UIApplication sendAction:to:from:forEvent:] ()
#6  0x00019792 in -[UIApplication sendAction:toTarget:fromSender:forEvent:] ()
#7  0x000be2aa in -[UIControl sendAction:to:forEvent:] ()
#8  0x000be773 in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
#9  0x000bda3e in -[UIControl touchesEnded:withEvent:] ()
#10 0x0003eb04 in -[UIWindow _sendTouchesForEvent:] ()
#11 0x0003ed2d in -[UIWindow sendEvent:] ()
#12 0x0002550c in -[UIApplication sendEvent:] ()
#13 0x00018d00 in _UIApplicationHandleEvent ()
#14 0x00deb7fe in PurpleEventCallback ()
#15 0x01123435 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#16 0x01087d72 in __CFRunLoopDoSource1 ()
#17 0x0108665a in __CFRunLoopRun ()
#18 0x01085b04 in CFRunLoopRunSpecific ()
#19 0x01085a1b in CFRunLoopRunInMode ()
#20 0x00de9f67 in GSEventRunModal ()
#21 0x00dea02c in GSEventRun ()
#22 0x00016cf2 in UIApplicationMain ()
#23 0x00001ed8 in main (argc=1, argv=0xbfffed94) at /Users/Alec/Desktop/Blackjack/Blackjack/main.m:16
#24 0x00001e35 in start ()
Alec
  • 919
  • 2
  • 10
  • 25

2 Answers2

5

First of all, you should post the stack trace of the error so we can be sure the error is happening in that code. You may need to set an exception breakpoint to get the stack trace where the error happened.

Second, the error message is telling you that you are sending the setImage: message to a UIRoundedRectButton object. If the error is happening in the code you posted, then the problem is that cardView is a UIButton, not a UIImageView. You are looping over all of the subviews of self.view. So we know that at least one of those subviews is a UIButton, not a UIImageView.

Also, your nested loops look suspicious. I suspect you are trying to do something like this:

- (IBAction)showCard:(id)sender {
    int i = 0;
    for (UIView *view in self.view.subviews) {
        if (![view isKindOfClass:[KCCardView class]])
            continue;
        KCCardView *cardView = (KCCardView *)view;
        cardView.image = [[dealerHand objectAtIndex:i++] cardImage];
    }
}

That's not really a great way to do it either, but it might work for you. A better way would be to give your view controller a separate NSArray that contains references to just the KCCardView instances.

Community
  • 1
  • 1
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • The app crashes no matter what the change is. – Alec Feb 21 '12 at 21:24
  • See I have 4 KCCardView's in the view. 2 are the dealer and the other 2 are the player's. By the way it looks in my code, I am affecting all KCCardView's and not just the dealer's. How could I make it just the dealer's cards that are affected? – Alec Feb 21 '12 at 21:29
  • 1
    Really? Every possible change results in a crashing app? That sounds unlikely. You need to post the stack trace of the exception. You need to describe what subviews are in your view. If you changed your code, you need to post the new code. – rob mayoff Feb 21 '12 at 21:30
  • The subviews are 4 KCCardView's (UIImageView's) and one UIButton, (showCard:). I put the code back to my old code. – Alec Feb 21 '12 at 21:35
  • @AlecK. "How could I make it just the dealer's cards that are affected?" By doing what rob suggested and adding the dealer cards to an `NSArray` `IBOutletCollection` then iterate through that instead of all of your subviews. – NJones Feb 21 '12 at 21:43
  • I have gone to these for methods because I have a BOOL in each KCCard class called isShowing and on only one dealer card it is NO. When I set the showCard to just set isShowing to YES, nothing happens because it doesn't update the image. – Alec Feb 21 '12 at 21:43
  • You need to set an exception breakpoint to get the real backtrace of the exception. – rob mayoff Feb 21 '12 at 21:50
0

You do realize the setImage message is being sent to a UIRoundedRectButton instance instead of a UIImageView, right ?

You could try:

  • Use [cardView setImage:aCard.cardImage forState:UIControlStateNormal];
  • Verify your cardView is actually a UIImageView and then keeping your code as is.
Ignacio Inglese
  • 2,605
  • 16
  • 18
  • I did realize that and attempted multiple changes, but nothing. There is no forState: in UIImageView. – Alec Feb 21 '12 at 21:20
  • Yes, it is a UIImageView, not a UIButton. – Alec Feb 21 '12 at 21:30
  • You should check what you added to your view. You're probably better off holding all the UIImageViews in a specific NSArray and looping through it, knowing that no other object is held in there. – Ignacio Inglese Feb 21 '12 at 21:30
  • Maybe I should. I'll take a look in a little bit. – Alec Feb 21 '12 at 21:35