Reputation: 101
I have an application in which i use this code for save data in local 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;
}
}
} When i run the application then i get error message which i shown below.
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIControlTargetAction setInteger:forKey:]: unrecognized selector sent to instance 0x181dd0' * Call stack at first throw: ( 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(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 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 ) terminate called after throwing an instance of 'NSException'
What is error in above code?
Thanks in advances...
Upvotes: 0
Views: 307
Reputation: 2144
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;
}
}
}
Upvotes: 0
Reputation: 7976
You are declaring your defaults variable inside of the switch. Move it to outside the switch
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
switch ( ((UIButton*)sender).tag ){
Upvotes: 2