我正在尝试将UIImageView的图像设置为等于某个图像:
- (IBAction)showCard:(id)sender {
for (KCCardView *cardView in self.view.subviews) {
for (KCCard *aCard in dealerHand) {
cardView.image = aCard.cardImage;
}
}
}
但所有发生的事情都是应用程序崩溃,并将以下内容记录到控制台。我似乎无法找到这个错误。
-[UIRoundedRectButton setImage:]: unrecognized selector sent to instance
谢谢!
(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 ()
答案 0 :(得分:5)
首先,您应该发布错误的堆栈跟踪,以便我们可以确定错误发生在该代码中。您可能需要set an exception breakpoint来获取发生错误的堆栈跟踪。
其次,错误消息告诉您正在向setImage:
对象发送UIRoundedRectButton
消息。如果您发布的代码中发生错误,则问题是cardView
是UIButton
,而不是UIImageView
。您正在遍历self.view
的所有子视图。因此,我们知道这些子视图中至少有一个是UIButton
,而不是UIImageView
。
此外,您的嵌套循环看起来很可疑。我怀疑你正试图做这样的事情:
- (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];
}
}
这也不是一个很好的方式,但它可能对你有用。更好的方法是为视图控制器提供单独的NSArray
,其中仅包含对KCCardView
个实例的引用。
答案 1 :(得分:0)
您确实意识到setImage
消息正在发送到UIRoundedRectButton
个实例而不是UIImageView
,对吗?
你可以尝试:
[cardView setImage:aCard.cardImage forState:UIControlStateNormal];
cardView
实际上是UIImageView
,然后保持代码原样。