代码之家  ›  专栏  ›  技术社区  ›  Ben Scheirman

在UINavigationController中来回导航会导致崩溃

  •  0
  • Ben Scheirman  · 技术社区  · 17 年前

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

    基本上,在第一个表中选择一个项目会成功创建&按下子viewcontroller,后退按钮工作正常。但是当我再次尝试选择该项目时,我在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];
        }
    }
    

    我不需要保留视图控制器,所以每次都创建它,完成后再释放它。

    [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 回复  |  直到 17 年前
        1
  •  2
  •   Joe Cannatti    17 年前

    我认为这可能与记忆管理有关。与其将viewController用作局部变量,不如将其设置为属性,并放弃该版本。

    @property(nonatomic, retain) UIViewController *viewController;
    

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

    @synthesize viewcontroller;
    

    并使用点语法访问它。

    self.viewController
    

    这将释放旧控制器(如果存在),并确保在导航控制器需要时该对象仍在周围。