我有一个应用程序,我使用此代码在本地prefences中保存数据。
-(IBAction)radio_button:(id)sender{
switch ( ((UIButton*)sender).tag ){
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
case 0:
{
[button0 setBackgroundImage:[UIImage imageNamed:@"checkBox_Checked.png"] forState:UIControlStateNormal];
button0.selected=TRUE;
[defaults setInteger: 0 forKey:@"tag_button0"];
[defaults synchronize];
NSLog(@"Data saved");
break;
}
case 1:
{
[button1 setBackgroundImage:[UIImage imageNamed:@"checkBox_Checked.png"] forState:UIControlStateNormal];
button1.selected=TRUE;
[defaults setInteger:1 forKey:@"tag_button1"];
[defaults synchronize];
NSLog(@"Data saved");
break;
}
case 2:
{
[button2 setBackgroundImage:[UIImage imageNamed:@"checkBox_Checked.png"] forState:UIControlStateNormal];
button2.selected=TRUE;
[defaults setInteger:2 forKey:@"tag_button2"];
[defaults synchronize];
NSLog(@"Data saved");
break;
}
}
} 当我运行应用程序然后我收到错误消息,我在下面显示。
*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [UIControlTargetAction setInteger:forKey:]:无法识别的选择器发送到实例0x181dd0' * 在第一次投掷时调用堆栈: ( 0 CoreFoundation 0x35f08c7b exceptionPreprocess + 114 1 libobjc.A.dylib 0x30186ee8 objc_exception_throw + 40 2 CoreFoundation 0x35f0a3e3 - [NSObject(NSObject)doesNotRecognizeSelector:] + 98 3 CoreFoundation 0x35eaf467 __ 转发 + 506 4 CoreFoundation 0x35eaf220 _CF_forwarding_prep_0 + 48 5 Congnitive_Appointment_Timer 0x0000d03d - [Set_Routine_Screen radio_button:] + 252 6 CoreFoundation 0x35eada43 - [NSObject(NSObject)performSelector:withObject:withObject:] + 26 7 UIKit 0x3384af20 - [UIApplication sendAction:to:from:forEvent:] + 136 8 UIKit 0x3384ae88 - [UIApplication sendAction:toTarget:fromSender:forEvent:] + 40 9 UIKit 0x3384ae50 - [UIControl sendAction:to:forEvent:] + 52 10 UIKit 0x3384aaa0 - [UIControl(内部)_sendActionsForEvents:withEvent:] + 536 11 UIKit 0x3384b5cc - [UIControl touchesEnded:withEvent:] + 460 12 UIKit 0x3383ceb0 - [UIWindow _sendTouchesForEvent:] + 588 13 UIKit 0x3383c4e4 - [UIWindow sendEvent:] + 396 14 UIKit 0x3381fc9c - [UIApplication sendEvent:] + 452 15 UIKit 0x3381f3b4 _UIApplicationHandleEvent + 6824 16 GraphicsServices 0x35262c88 PurpleEventCallback + 1048 17 CoreFoundation 0x35e9a5cb CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 28 18 CoreFoundation 0x35e9a589 __CFRunLoopDoSource1 + 164 19 CoreFoundation 0x35e8c835 __CFRunLoopRun + 580 20 CoreFoundation 0x35e8c50b CFRunLoopRunSpecific + 226 21 CoreFoundation 0x35e8c419 CFRunLoopRunInMode + 60 22 GraphicsServices 0x35261d24 GSEventRunModal + 196 23 UIKit 0x3386557c - [UIApplication _run] + 588 24 UIKit 0x33862558 UIApplicationMain + 972 25 Congnitive_Appointment_Timer 0x00002c19 main + 72 26 Congnitive_Appointment_Timer 0x00002b98 start + 52 ) 抛出'NSException'实例后调用终止
上述代码中的错误是什么?
谢谢你的进步......
答案 0 :(得分:2)
您在交换机内声明了默认变量。将其移至开关外部
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
switch ( ((UIButton*)sender).tag ){
答案 1 :(得分:0)
use This One
-(IBAction)radio_button:(id)sender{
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
switch ( ((UIButton*)sender).tag ){
case 0:
{
btn1.selected=TRUE;
[defaults setInteger:0 forKey:@"tag_button0"];
[defaults synchronize];
NSLog(@"Data saved");
break;
}
case 1:
{
btn2.selected=TRUE;
[defaults setInteger:1 forKey:@"tag_button1"];
[defaults synchronize];
NSLog(@"Data saved");
break;
}
case 2:
{
btn3.selected=TRUE;
[defaults setInteger:2 forKey:@"tag_button2"];
[defaults synchronize];
NSLog(@"Data saved");
break;
}
}
}