技巧.h 文件
#import "Tricks.h"
@implementation Tricks
static NSMutableArray *trickList = nil;
+(NSMutableArray *)trickList
{
if(!trickList){
trickList = [[NSMutableArray alloc]init];
}
return trickList;
}
@end
技巧.m 文件
@interface Tricks : NSObject
@property(strong, nonatomic) NSString *trickName;
将对象添加到数组的方法
-(IBAction)saveAction:(id)sender
{
Tricks *trick = [[Tricks alloc]init];
trick.trickName = self.trickLabel.text;
[[Tricks trickList]insertObject:trick atIndex:0];
[self.navigationController popViewControllerAnimated:YES];
}
在 UITabelview 类的 .h 文件中,我引用了技巧类,但我确信这一行有错误。
@property (strong, nonatomic) Tricks *tricks;
在 cellForRow 方法中,我正在存储数据
_trick = [[NSMutableDictionary alloc]initWithObjectsAndKeys:trick,nil];
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:_trick forKey:@"numberArray"];
[defaults synchronize];
NSLog(@"%@",_trick);
在 viewDidLoad 的 UITableview 的 .m 类中,我想检索数据
if([[NSUserDefaults standardUserDefaults] objectForKey:@"numberArray"] != nil) {
_tricks = [[NSUserDefaults standardUserDefaults] objectForKey:@"numberArray"];
}