2009-06-08 70 views
0

我得到一个一致的崩溃,当我创建一个使用导航控制器的简单应用程序。导航背部,向前一个UINavigationController导致崩溃

基本上选择在第一个表中的项目成功创建&推子视图 - 控制和后退按钮的工作就好了。但是,当我尝试再次选择该项目时,我在GDB中发生了一次奇怪的崩溃。我没有得到任何错误,只是调试器吐出一些信息,并且应用程序挂起。

此处,我推视图控制器:

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

    UIViewController *viewController; 

    NSString *selection = [items objectAtIndex:indexPath.row]; 
    if([selection isEqualToString:@"Artists"]) { 
     NSLog(@"Selected artists"); 
     if(artistsViewController == nil) { 
      NSLog(@"creating ArtistsViewController"); 
      artistsViewController = [[ArtistsViewController alloc] init]; 
     } 

     viewController = artistsViewController; 
    } 

    if(viewController != nil) { 
     [self.navigationController pushViewController:viewController animated:YES]; 
     [viewController release]; 
    } 
} 

我有没有必要保持周围的视图控制器,所以我简单地创建它的每一个时间和释放它,当我完了。

这里是无益的控制台日志。请注意如何它说:“选定的艺术家”,然后选择“创建ArtistsViewController”第一次,那么第二次它说:“选定的艺术家”,然后死了:

[Session started at 2009-06-08 18:00:16 -0500.] 
2009-06-08 18:00:18.856 Pocket Tabs[96726:20b] View did load 
2009-06-08 18:00:18.862 Pocket Tabs[96726:20b] loading data for tableview 
2009-06-08 18:00:20.265 Pocket Tabs[96726:20b] Selected artists 
2009-06-08 18:00:20.265 Pocket Tabs[96726:20b] creating ArtistsViewController 

[Session started at 2009-06-08 18:00:22 -0500.] 
2009-06-08 18:00:22.061 Pocket Tabs[96726:20b] Selected artists 
Loading program into debugger… 
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008) 
Copyright 2004 Free Software Foundation, Inc. 
GDB is free software, covered by the GNU General Public License, and you are 
welcome to change it and/or distribute copies of it under certain conditions. 
Type "show copying" to see the conditions. 
There is absolutely no warranty for GDB. Type "show warranty" for details. 
This GDB was configured as "i386-apple-darwin".warning: Unable to read symbols for "/System/Library/Frameworks/UIKit.framework/UIKit" (file not found). 
warning: Unable to read symbols from "UIKit" (not yet mapped into memory). 
warning: Unable to read symbols for "/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics" (file not found). 
warning: Unable to read symbols from "CoreGraphics" (not yet mapped into memory). 
Program loaded. 
sharedlibrary apply-load-rules all 
Attaching to program: `/Users/ben/Library/Application Support/iPhone Simulator/User/Applications/F063AD87-BEFE-4CB9-AE26-E7149C8D7D4C/Pocket Tabs.app/Pocket Tabs', process 96726. 
(gdb) 

任何想法?我很难过。

编辑:我想通了。看起来我正在将此分配给一个实例变量,以避免重新分配子视图,我必须切换齿轮并忘记它。消除实例变量artistsController解决了它。

回答

2

我想这可能是与内存管理。而不是使用viewController作为局部变量,将其设置为属性并摆脱该版本。

@property(nonatomic, retain) UIViewController *viewController; 

合成器的getter和setter(在.m文件)

@synthesize viewcontroller; 

,并使用点语法来访问它。

self.viewController 

这将释放旧的控制器如果一个存在,并确保该对象仍然存在时,导航控制器需要它的人。

+0

我能做到这一点,但我真的很想知道为什么我的代码不能正常工作。否则,我只是巧合编程... :) – 2009-06-09 00:49:29