2010-02-13 113 views
0

我得到一个NSArray,它被填充到我的UITableViewController的init方法中。对象崩溃应用程序

我在“didSelectRowAtIndexPath”中使用此对象来推送另一个tableviewcontroller。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
ablogSingleCatTableViewController *singleCatTableViewController = [[ablogSingleCatTableViewController alloc] initWithStyle:UITableViewStylePlain category:[categories objectAtIndex:indexPath.row]]; 
[[self navigationController] pushViewController:singleCatTableViewController animated:YES]; 
[singleCatTableViewController release]; 
} 

这个工作几次,当我开始我的申请。在选择一行并返回到rondom点的主uitableview控制器后,我的应用程序在选择行后崩溃。

我发现一些nslogs,它崩溃,如果我尝试使用我的“类别”对象。

所以

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

NSLog(@"before"); 
NSLog(@"cats: %@", categories); 
NSLog(@"after"); 

ablogSingleCatTableViewController *singleCatTableViewController = [[ablogSingleCatTableViewController alloc] initWithStyle:UITableViewStylePlain category:[categories objectAtIndex:indexPath.row]]; 
[[self navigationController] pushViewController:singleCatTableViewController animated:YES]; 
[singleCatTableViewController release]; 

} 

与代码我的应用程序崩溃后的“前” ...“后,”一直没有出现。 我不知道为什么我的“类别”对象崩溃我的应用程序?!

我的类别对象是在我的头文件中定义的,并具有@property(nonatomic,retain)。我合成它并在我的dealloc方法中释放它。

任何人都有想法?

//编辑:

因为意见

这里一些更多的细节,:

调试器控制台说:“程序接收到的信号:‘EXC_BAD_ACCESS’

我创建类别数组像这样:

- (void)initCategories { 
NSString *path = [[NSBundle mainBundle] pathForResource:@"Categories" ofType:@"plist"]; 
[self setCategories:[[NSArray alloc] initWithContentsOfFile:path]]; 
    } 

调用此方法在我initwithstyle方法

[self initCategories]; 

我的其他自定义初始化方法看起来是这样的:

- (id)initWithStyle:(UITableViewStyle)style category:(NSDictionary*)cat { 
if (self = [super initWithStyle:style]) { 
    currentCategory = cat; 

    items = [[NSMutableArray alloc] init]; 

    self.title = [currentCategory objectForKey:@"TITLE"]; 
    //XLog("%@", currentCategory); 
} 
return self; 
} 
+0

你能否提供关于碰撞的更多细节?如果你调试应用程序而不是运行它(cmd-Y和cmd-R),你通常会让调试器在崩溃时暂停执行。这样,您可以检查确切的回溯并查看当时任何实例变量的值。 – 2010-02-13 19:59:22

+0

显示你创建分类数组的代码请 – willcodejavaforfood 2010-02-13 19:59:39

+0

我觉得你在ablogSingleCatTableViewController initWithStyle:category:method中有问题。你能发布你的代码吗? – EEE 2010-02-13 20:55:32

回答

1

好的,第一件事是;

[self setCategories:[[NSArray alloc] initWithContentsOfFile:path]]; 

你在这里有泄漏。只是使用

categories = [[NSArray alloc] initWithContentsOfFile:path]; 

此处发生崩溃;

currentCategory = cat; 

你必须保留,使用;

currentCategory = [cat retain]; 

这些都是我在发布代码中看到的,如果你没有在程序的其余部分的任何错误的问题,它应该是罚款与这些修复。

+0

令人惊讶的是它保留了我的“猫”后现在适用于我。im新的objective-c和新的语言,其中ih大大保留和释放对象,所以即时通讯总是有点困惑,不知道什么时候做这件事情,特别是保留,释放工程相当好:D 另一件事,为什么我有“setCategories”泄漏?我的意思是,我作为一个属性获得类别,所以我得到了一个setter方法。但再次感谢!它解决了。 – choise 2010-02-14 00:20:09

+0

你应该从苹果的文档开始,看到这篇文章http://stackoverflow.com/questions/106627/memory-management-in-objective-c,你可以找到有用的链接。学习内存管理非常重要,从此开始,编码将比您想象的更容易。祝你好运。 – EEE 2010-02-14 00:27:44

+0

在“setCategories”中,您已经使用@property(nonatomic,retain)增加了类别bty的保留计数。但是你也有[[NSArray alloc] init ..],这个保留但是永远不会被释放,所以你会有泄漏。你可以编写[self setCategories:[[NSAray alloc] init ...] autorelease],通过这种方式分配的nsarray会自动发布,但这样使用没有意义。实际上你应该只使用self.categories = [NSArray arrayWithContentOfFile:path]; ,这将保留类别的数量并设置内容。 – EEE 2010-02-14 00:34:24

1

如果你创建你的阵列是这样的:

NSArray *tmpArray = [[NSArray alloc] initWithBlah ...]; 

请确保您使用合成的吸气剂由它分配使用此代码:

self.categories = tmpArray; 
[tmpArray release]; 

如果你这样做:

categories = tmpArray; 
[tmpArray release]; 

实例变量将根本不被保留。

+0

我的方式: 的NSString *路径= [[一个NSBundle mainBundle] pathForResource:@ “类别” ofType:@ “的plist”]; \t类别= [[NSArray的页头] initWithContentsOfFile:路径]; – choise 2010-02-13 20:06:28

+0

改成self.categories,它会起作用,就像我在我的帖子中所做的那样, – willcodejavaforfood 2010-02-13 20:39:10

+0

它没有,hm – choise 2010-02-13 21:20:59