我有一个 addToFavourites 函数,它导致 EXC_BAD_ACCESS 错误。基本上,我有一个收藏夹视图控制器,其中包含用户之前添加的所有收藏夹的列表,现在当单击其中一个收藏夹时,它将视图推送到 detailViewController,这工作正常。在详细视图控制器上,我有一个添加到收藏夹按钮,如果它检测到该键已存在于 addToFavourites 字典中,则此按钮将更改为从收藏夹中删除。当用户从收藏夹页面以及列出所有详细视图的主页访问此 detailViewController 时,就会出现问题。他们能够做到这一点,因为我有一个标签栏。
所以说我在通过收藏页面访问的detailView中,收藏按钮有“从收藏中删除”,这是正确的。但是然后说我单击从收藏夹中删除按钮,该按钮更改为“添加到收藏夹”并将其从字典中删除。到目前为止一切正常。现在,当我切换到相同的 detailView,但这次从标签栏上的不同标签访问时,首先收藏夹按钮仍然显示“从收藏夹中删除”,当我第一次单击此按钮时,它变为“添加到favourites”,然后当我再次单击它时,我得到这个 EXC_BAD_ACCESS 错误。
这是 addToFavouritesFunction:
- (IBAction)addToFavourites:(id)sender {
NSString *type = [[NSUserDefaults standardUserDefaults]objectForKey:@"type"];
if(type == @"v") {
NSString *area = [[NSUserDefaults standardUserDefaults]objectForKey:@"area"];
NSString *ID1 = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID1"];
if([[addToFavouritesDictionary allKeys] containsObject:ID1]) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/SavedDict.data"];
[addToFavouritesDictionary removeObjectForKey:ID1];
[favouritesButton setTitle:@"+ Favourites" forState:(UIControlState)UIControlStateNormal];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(@"New Dictionary: %@", addToFavouritesDictionary);
} else {
NSString *ID1 = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID1"];
[addToFavouritesDictionary setObject:Name forKey:ID1];
[favouritesButton setTitle:@"- Favourites" forState:(UIControlState)UIControlStateNormal];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/SavedDict.data"];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(@"Mutable Dictionary: %@", addToFavouritesDictionary);
//[addToFavouritesDictionary release];
}
} else {
//NSString *area = [[NSUserDefaults standardUserDefaults]objectForKey:@"area"];
NSString *ID2 = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID2"];
if([[addToFavouritesDictionary allKeys] containsObject:ID2]) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/SavedDict.data"];
[addToFavouritesDictionary removeObjectForKey:ID2];
[favouritesButton setTitle:@"+ Favourites" forState:(UIControlState)UIControlStateNormal];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(@"Dictionary: %@", addToFavouritesDictionary);
} else {
NSString *ID2 = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID2"];
[addToFavouritesDictionary setObject:Name forKey:ID2];
[favouritesButton setTitle:@"- Favourites" forState:(UIControlState)UIControlStateNormal];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/SavedDict.data"];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(@"Mutable Dictionary: %@", addToFavouritesDictionary);
}
}
}
谢谢你的帮助!