Store data in tableview to NSUserDefaults
- by Jozef Vrana
Tricks.h file
#import "Tricks.h"
@implementation Tricks
static NSMutableArray *trickList = nil;
+(NSMutableArray *)trickList
{
if(!trickList){
trickList = [[NSMutableArray alloc]init];
}
return trickList;
}
@end
Tricks.m file
@interface Tricks : NSObject
@property(strong, nonatomic) NSString *trickName;
Method for adding objects to array
-(IBAction)saveAction:(id)sender
{
Tricks *trick = [[Tricks alloc]init];
trick.trickName = self.trickLabel.text;
[[Tricks trickList]insertObject:trick atIndex:0];
[self.navigationController popViewControllerAnimated:YES];
}
In .h file of UITabelview class I am making a reference to tricks class, but I am sure there is error on this line.
@property (strong, nonatomic) Tricks *tricks;
In cellForRow method I am storing data
_trick = [[NSMutableDictionary alloc]initWithObjectsAndKeys:trick,nil];
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:_trick forKey:@"numberArray"];
[defaults synchronize];
NSLog(@"%@",_trick);
In .m class of UITableview in viewDidLoad I want to retrieve data
if([[NSUserDefaults standardUserDefaults] objectForKey:@"numberArray"] != nil) {
_tricks = [[NSUserDefaults standardUserDefaults] objectForKey:@"numberArray"];
}
Thanks for advices