首页 文章

Iphone应用程序因未捕获的异常而终止'NSInvalidArgumentException'

提问于
浏览
0

我有一个应用程序,我使用此代码在本地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 __forwarding 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 - [UI控制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主72 26 Congnitive_Appointment_Timer 0x00002b98开始52)抛出'NSException'实例后终止调用

上面代码中的错误是什么?

谢谢你的进步......

2 回答

  • 2

    您在交换机内声明了默认变量 . 将其移至开关外部

    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    switch ( ((UIButton*)sender).tag ){
    
  • 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;
            } 
        }
    }
    

相关问题