0

我有一个应用程序,我在其中使用此代码将数据保存在本地首选项中。

-(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 0x3384affor0-[应用程序 UI 发送 0x3384affor2 :] + 136 8 UIKit 0x3384ae88 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 40 9 UIKit 0x3384ae50 -[UIControl sendAction:to:forEvent:] + 52 10 UIKit 0x3384aaa0 -[UIControl(Internal) _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] + 452 15 UIKit 0x3381f3b4 _UIApplicationHandleEvent + 6824 16 GraphicsServices 0x35262c88 PurpleEventCallback + 1048 17 CoreFoundation 0x35e9a5cb] + 452 15 UIKit 0x3381f3b4 _UIApplicationHandleEvent + 6824 16 GraphicsServices 0x35262c88 PurpleEventCallback + 1048 17 CoreFoundation 0x35e9a5cbCFRUNLOOP_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”实例后调用终止

上面的代码有什么错误?

提前感谢...

4

2 回答 2

2

您在开关内声明您的默认变量。把它移到开关外面

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
switch ( ((UIButton*)sender).tag ){
于 2011-11-04T05:11:07.060 回答
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;
        } 
    }
}
于 2011-11-04T05:11:25.420 回答