导航回来&在UINavigationController中转发会导致崩溃

时间:2009-06-08 23:09:50

标签: iphone cocoa-touch

当我创建一个使用导航控制器的简单应用程序时,我遇到了一致的崩溃。

基本上选择第一个表中的项目会成功创建&推动子视图控制器,后退按钮工作正常。但是当我再次尝试选择该项时,我在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解决了它。

1 个答案:

答案 0 :(得分:2)

我认为这可能与记忆管理有关。不要将viewController用作局部变量,而是将其设置为属性并摆脱释放。

@property(nonatomic, retain) UIViewController *viewController;

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

@synthesize viewcontroller;

并使用点语法来访问它。

self.viewController

这将释放旧控制器(如果存在)并确保在Nav Controller需要时该对象仍然存在。