// I wrote this function in Separate class file:
-(void)Display
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger *Position = [prefs integerForKey:@"currentlevel"];
NSLog(@"------->%i",Position);
//This NSLog is returning Value 6
[prefs synchronize];
}
//In my viewController I am calling this function using object I have created for my class
@interface ViewController ()
{
//display My class name
//disp is object fro my class
display *disp;
}
@implementation ViewController
- (void)viewDidLoad
{
disp = [[display alloc]init];
[disp Display];
NSLog(@"%i",[disp Position]);
//this NSLog statement returning value 0
[super viewDidLoad];
}
If anyone has any solution please help me.
Try to implement like this..
@implementation ClassA
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSUserDefaults standardUserDefaults]setInteger:100 forKey:@"currentlevel"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
@end
@implementation ClassB
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
int B = [[[NSUserDefaults standardUserDefaults] integerForKey:@"currentlevel"] integerValue];
NSLog(@"Interger Value %d",b);
}
Why not get the value from NSUserDefaults in the view controller itself? Like this:
-(void) viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger *Position = [prefs integerForKey:@"currentlevel"];
NSLog(@"------->%i",Position);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With