添加到收藏夹功能iPhone

时间:2011-05-15 23:01:46

标签: iphone favorites

我正在尝试在我的应用上使用“添加到收藏夹”功能。我似乎无法让它正常工作。基本上每次我的手机重新启动时,所有收藏夹都会从阵列和字典中删除。无论如何都要保存这些数据,以便每次应用程序启动时都能保存和恢复它?非常感谢。

以下是一些代码:在appDidFinishLaunching中:

//============== Add To Favourites ==============

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Saved.data"];


NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryArray = [pathsArray objectAtIndex:0];
NSString *filePathArray = [documentsDirectoryArray stringByAppendingPathComponent:@"savedArray.data"];

delegateFavouritesDictionary = [NSMutableDictionary dictionary];
[delegateFavouritesDictionary writeToFile:filePath atomically:YES];

    delegateFavouritesArray = [[NSMutableArray alloc]init];

在detailViewController viewDidLoad中:

self.addToFavouritesArray = [[NSMutableArray alloc] init];
self.addToFavouritesDictionary = [NSMutableDictionary dictionary];
TabBar_NavigationBasedAppDelegate *mainDelegate = (TabBar_NavigationBasedAppDelegate *)[[UIApplication sharedApplication]delegate];
//addToFavouritesArray = [[NSMutableArray alloc] init];
NSMutableArray *tempArray1 = mainDelegate.delegateFavouritesArray;
//NSMutableDictionary *tempDictionary1 = mainDelegate.delegateFavouritesDictionary;
addToFavouritesArray = tempArray1;

//self.addToFavouritesDictionary = tempDictionary1;


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Saved.data"];
addToFavouritesDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

在detailViewController中,在addToFavourites函数中:

NSString *ID = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID"];

    if([[addToFavouritesDictionary allKeys] containsObject:ID]) {

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Saved.data"];



        [addToFavouritesDictionary removeObjectForKey:ID];
        [addToFavouritesArray removeObject:Name];
        [favouritesButton setTitle:@"+ Favourites" forState:(UIControlState)UIControlStateNormal];
        [addToFavouritesDictionary writeToFile:filePath atomically: YES];
        NSLog(@"New Dictionary: %@", addToFavouritesDictionary);

    } else {

        [addToFavouritesArray addObject:Name];
        NSString *ID = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID"];
        [addToFavouritesDictionary setObject:Name forKey:ID];
        [favouritesButton setTitle:@"- Favourites" forState:(UIControlState)UIControlStateNormal];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Saved.data"];

        [addToFavouritesDictionary writeToFile:filePath atomically: YES];
        NSLog(@"Mutable Dictionary: %@", addToFavouritesDictionary);
        //[addToFavouritesDictionary release];

    }

在FavouritesViewController中,在viewDidLoad中:

TabBar_NavigationBasedAppDelegate *mainDelegate = (TabBar_NavigationBasedAppDelegate *)[[UIApplication sharedApplication]delegate];


favouritesArray = [[NSMutableArray alloc] init];

NSMutableArray *tempArray1 = mainDelegate.delegateFavouritesArray;
favouritesArray = tempArray1;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Saved.data"];


favouritesDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

在你的applicationDidFinishLaunching:方法中(所以每当你的应用程序启动时),你首先创建一个空的NSMutableDictionary,然后将其写入Saved.data,可能会覆盖可能存在的任何内容。