综合技术

Save the data in tableview to NSUserDefaults

微信扫一扫,分享到朋友圈

Save the data in tableview to NSUserDefaults
0

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

You are losing all the data because you are trying to save Tricks thats is NSObject
and gets destroyed when your app gets killed, it works while the app is running and you create the NSUserDefaults
better don’t save it as Tricks objects, instead you can store your data as NSDictionary
, or NSString
which NSUserDefaults can store, and when you want to use it, you can create Trick
object using your saved NSDictionary
data.

EditOnly NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary can be stored using NSUserDefaults

阅读原文...


微信扫一扫,分享到朋友圈

Save the data in tableview to NSUserDefaults
0

Hello, buddy!

27.QT-QProgressBar动态实现多彩进度条(详解)

上一篇

dependencies with super classes

下一篇

评论已经被关闭。

插入图片

热门分类

往期推荐

Save the data in tableview to NSUserDefaults

长按储存图像,分享给朋友