0

我正在使用多個視圖控制器和導航控制器的應用程序。當應用程序運行並執行以下代碼時,它會在嘗試添加子視圖時引發異常。將UINavigationController添加到窗口時EXC_BAD_ACCESS

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    [self.window addSubview:[navigationController view]]; 
    [self.window makeKeyAndVisible]; 
} 

我聲明導航控制器像這樣:

@interface SimpleContactsAppDelegate : NSObject <UIApplicationDelegate> { 
    NSManagedObjectModel *managedObjectModel; 
    NSManagedObjectContext *managedObjectContext; 
    NSPersistentStoreCoordinator *persistentStoreCoordinator; 

    UIWindow *window; 
    UINavigationController *navigationController; 
    // view for adding new contacts 
    UIViewController *newContactView; 
    UIButton *addButton; 

    // controls for the addContactView 
    UIButton *saveContactButton; 
    UITextField *nameField; 
    UITextField *emailField; 
    UITextField *phoneField; 

    UITableView *contactsTable; 
} 
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel; 
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; 
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator; 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController; 

@property (nonatomic, retain) IBOutlet UIButton *addButton; 
@property (nonatomic, retain) IBOutlet UITableView *contactsTable; 

// controller and fields for the form 
@property (nonatomic, retain) IBOutlet UIViewController *newContactView; 
@property (nonatomic, retain) IBOutlet UITextField *nameField; 
@property (nonatomic, retain) IBOutlet UITextField *emailField; 
@property (nonatomic, retain) IBOutlet UITextField *phoneField; 
@property (nonatomic, retain) IBOutlet UIButton *saveContactButton; 

我已經連接在導航控制器在XIB的委託對象。隨時看看我的全部源:https://github.com/agmcleod/SimpleContacts/tree/parttwo

我試過用NSZombie儀器,但它似乎並沒有停下來,讓我檢查什麼特別出錯。它也有時會繼續運行,並且不會終止。我最終不得不使用活動終端強制退出它。

回答

3

首先,你聲明一個屬性,但通過它的實例變量訪問UINavigationController而不是使用屬性。

使用此:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    [self.window addSubview:[self.navigationController view]]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

其次,你改變了你的主要筆尖文件 「Window.xib」 的名稱。您必須將其更改回「MainWindow.xib」,否則您將不得不編輯SimpleContacts-Info.plist並將「Main nib文件基本名稱」的值更改爲「Window」。

+0

非常感謝! :d。這固定了它。重命名文件,並將自我添加到navigationController。 – agmcleod 2011-05-29 23:01:07