2009-06-08 50 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