运行addToFavourites函数时EXC_BAD_ACCESS

时间:2011-05-20 11:25:51

标签: iphone objective-c function favorites

我有一个addToFavourites函数,导致EXC_BAD_ACCESS错误。 基本上,我有收藏夹视图控制器,其中包含用户之前添加的所有收藏夹的列表,现在当点击其中一个收藏夹时,它将视图推送到detailViewController,这很好用。在详细视图控制器上,我有一个添加到收藏夹按钮,如果该按钮检测到该键已存在于addToFavourites字典中,则该按钮将更改为从收藏夹中删除。当用户从收藏夹页面以及列出所有detailViews的主页面访问此detailViewController时,会发生此问题。他们能够做到这一点,因为我有一个标签栏。

所以说我在通过收藏夹页面访问的detailView中,收藏夹按钮有“从收藏夹中删除”,这是正确的。但随后说我点击从收藏夹中移除按钮,按钮变为“添加到收藏夹”并将其从字典中删除。到目前为止一切正常。现在,当我切换到相同的detailView,但这次从标签栏上的不同选项卡访问时,首先收藏夹按钮仍然显示“从收藏夹中删除”,当我第一次单击此按钮时,它将更改为“添加到收藏夹“,然后当我再次点击它时,我收到此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);
        }
    }
}

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

检查调试器是否正确显示崩溃线...并在此之后设置断点以检查值

答案 1 :(得分:0)

嗯...您的addToFavouritesDictionary对象或您的Name对象已被解除分配。你是如何创建这些对象的?你应该在你应该之前释放它们,或者你没有保留它们。

Objective-C Memory Management

答案 2 :(得分: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];