开发者

Add to Favourites Function iPhone

开发者 https://www.devze.com 2023-03-06 06:57 出处:网络
I am trying to get an \"add to favourites\" feature working on my app. I just can\'t seem to get it working correctly. Basically everytime my phone restarts, all the favourites are deleted from the ar

I am trying to get an "add to favourites" feature working on my app. I just can't seem to get it working correctly. Basically everytime my phone restarts, all the favourites are deleted from the array and dictionary. Is there anyway to save this data so that it is kept and restored everytime the app launches? Many thanks.

Here is some of the code: in 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];

In the detailViewController viewDidLoad:

self.addToFavouritesArray = [[NSMutableArray alloc] init];
self.addToFavouritesDictionary = 开发者_C百科[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];

In the detailViewController, in the addToFavourites Function:

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];

    }

In the FavouritesViewController, in 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];

Many thanks for any help


In your applicationDidFinishLaunching: method (so every time your app launches), you're first creating an empty NSMutableDictionary and then writing that to Saved.data, potentially overwriting anything that might have been there.

0

精彩评论

暂无评论...
验证码 换一张
取 消