将UINavigationController添加到窗口时的EXC_BAD_ACCESS

时间:2011-05-29 22:46:46

标签: objective-c cocoa-touch ios uiviewcontroller uinavigationcontroller

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

- (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中的Delegate对象。请随意查看我的完整资料来源:https://github.com/agmcleod/SimpleContacts/tree/parttwo

我尝试过使用带有NSZombie的乐器,但它似乎并没有停下来让我检查出现了什么特别错误。它也只是有时保持运行,并且不会终止。我最终不得不强制使用活动终端退出它。

1 个答案:

答案 0 :(得分:3)

首先,您声明了一个属性,但是通过其实例变量而不是使用该属性来访问UINavigationController。

使用此:

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

其次,您将主nib文件的名称更改为“Window.xib”。您必须将其更改回“MainWindow.xib”,否则您必须编辑SimpleContacts-Info.plist并将“主nib文件基本名称”的值更改为“Window”。