将UINavigationController添加到窗口时EXC_BAD_ACCESS

我正在使用多个视图控制器和导航控制器的应用程序。 当应用程序运行并执行以下代码时,它会在尝试添加子视图时引发exception。

- (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使用仪器,但它似乎并没有停下来,让我检查什么特别是出了什么问题。 它也只是有时运行,不会终止。 我最终不得不使用活动terminal强制退出它。

首先,你声明一个属性,但通过它的实例variables访问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”。