这应该很简单,但我无法弄清楚出了什么问题。我正在创建一个tableview,我想要一个按钮,我可以单击以在已选中和未选中之间切换:
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:DiaperCellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:DiaperCellIdentifier] autorelease];
wetButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
image = [UIImage imageNamed:@"unchecked_large.png"];
CGRect frame = CGRectMake(150.0, 5.0, image.size.width, image.size.height);
wetButton.frame = frame;
[wetButton setBackgroundImage:image forState:UIControlStateNormal];
[wetButton addTarget:self action:@selector(wetClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:wetButton];
现在,当我单击此按钮时,我得到一个堆栈跟踪.... [NSCFString scale]:无法识别的选择器发送到实例....感谢您的帮助。
- (void) wetClicked:(id)sender{
if (isWet) {
isWet = NO;
[wetButton setBackgroundImage:@"unchecked_large.png" forState:UIControlStateNormal];
} else {
isWet = YES;
[wetButton setBackgroundImage:@"checked_large.png" forState:UIControlStateNormal];
}
}
这是跟踪:
2011-03-09 10:19:57.124 InfantCare [64064:207] - [NSCFString scale]:无法识别的选择器发送到实例0x33be0
2011-03-09 10:19:57.240 InfantCare [64064:207] * 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [NSCFString scale]:无法识别的选择器发送到实例0x33be0'
* 首次调用堆栈:
(
0 CoreFoundation 0x00f2dbe9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x010825c2 objc_exception_throw + 47
2 CoreFoundation 0x00f2f6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00e9f366 ___forwarding___ + 966
4 CoreFoundation 0x00e9ef22 _CF_forwarding_prep_0 + 50
5 UIKit 0x003d1e7b -[UIImageView setImage:] + 250
6 UIKit 0x004ea353 -[UIButton layoutSubviews] + 273
7 QuartzCore 0x01d58451 -[CALayer layoutSublayers] + 181
8 QuartzCore 0x01d5817c CALayerLayoutIfNeeded + 220
9 QuartzCore 0x01d5137c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
10 QuartzCore 0x01d510d0 _ZN2CA11Transaction6commitEv + 292
11 QuartzCore 0x01d817d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
12 CoreFoundation 0x00f0efbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
13 CoreFoundation 0x00ea40e7 __CFRunLoopDoObservers + 295
14 CoreFoundation 0x00e6cbd7 __CFRunLoopRun + 1575
15 CoreFoundation 0x00e6c240 CFRunLoopRunSpecific + 208
16 CoreFoundation 0x00e6c161 CFRunLoopRunInMode + 97
17 GraphicsServices 0x017cf268 GSEventRunModal + 217
18 GraphicsServices 0x017cf32d GSEventRun + 115
19 UIKit 0x0031642e UIApplicationMain + 1160
20 InfantCare 0x00002228 main + 102
21 InfantCare 0x000021b9 start + 53
)
在抛出'NSException'实例后终止调用
收到信号:“SIGABRT”。
答案 0 :(得分:3)
我认为在调用函数setBackgroundImage时需要使用UIImage实例作为参数:
[wetButton setBackgroundImage:@"unchecked_large.png" forState:UIControlStateNormal];
改为使用:
[wetButton setBackgroundImage:[UIImage imageNamed:@"unchecked_large.png"] forState:UIControlStateNormal];
答案 1 :(得分:2)
您发布的代码很好。问题很可能出在wetClicked:
方法中,您在NSString上调用scale
方法。
现在你发布了wetClicked:
和跟踪,我看到了问题:你正在将字符串而不是图像传递给setBackgroundImage:forState:
。试试这个:
- (void) wetClicked:(id)sender{
if (isWet) {
isWet = NO;
[wetButton setBackgroundImage:[UIImage imageNamed:@"unchecked_large.png"] forState:UIControlStateNormal];
} else {
isWet = YES;
[wetButton setBackgroundImage:[UIImage imageNamed:@"checked_large.png"] forState:UIControlStateNormal];
}
}