0

我有一个 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);
        }
    }
}

谢谢你的帮助!

4

3 回答 3

0

好吧...您的 addToFavouritesDictionary 对象或您的 Name 对象已被释放。你是如何创建这些对象的?要么你提前释放它们,要么你一开始就没有保留它们。

Objective-C 内存管理

于 2011-05-20T12:12:04.090 回答
0

您可能不应该使用以下行:

if(type == @"v") {

您在这里所做的是将变量type的指针与字符串的指针进行比较@"v"。相反,您应该使用比较字符串内容的方法:

if ([type isEqualToString:@"v") {

编辑

保持标题不变的最简单方法是将文本存储在 NSUserDefaults 中。每当按下按钮时,您都可以执行以下操作:

// For "Add to Favorites"
[[NSUserDefaults standardUserDefaults] setObject:@"+ Favorites" forKey:@"ButtonState"];

// For "Remove from Favorites"
[[NSUserDefaults standardUserDefaults] setObject:@"- Favorites" forKey:@"ButtonState"];

然后,当视图加载时,您将执行以下操作:

[favoritesButton setTitle:(NSString*)[[NSUserDefaults standardUserDefaults] objectForKey:@"ButtonState"] forState:UIControlStateNormal];
于 2011-05-20T12:18:17.597 回答
0

使用调试器检查它是否准确地显示崩溃行...并在此之后设置断点以检查值

于 2011-05-20T11:47:02.037 回答