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