将UINavigationController添加到窗口时EXC_BAD_ACCESS [英] EXC_BAD_ACCESS when adding UINavigationController to window

查看:50
本文介绍了将UINavigationController添加到窗口时EXC_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on an app using multiple view controllers and the navigation controller. When the application runs and executes the following code, it throws an exception when trying to add the sub view.

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

我这样声明导航控制器:

I declare the navigation controller like so:

@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

I have connected the navigation controller to the Delegate object in the XIB. Feel free to have a look at my full source: https://github.com/agmcleod/SimpleContacts/tree/parttwo

我尝试将Instruments与NSZombie一起使用,但是它似乎并没有停止,而是让我检查出特别出问题的地方。它有时也会保持运行,并且不会终止。我最终不得不使用活动终端强制退出它。

I've tried using Instruments with NSZombie, but it doesnt seem to stop and let me inspect what has particularly gone wrong. It also just keeps running sometimes, and won't terminate. I end up having to force quit it using active terminal.

推荐答案

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

First of all, you declare a property, but access the UINavigationController through its instance variable instead of using the property.

使用此方法:

- (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并将 Main nib文件库名称的值更改为 Window。

Second, you changed the name of your main nib file to "Window.xib". You either have to change it back to "MainWindow.xib" or you will have to edit your SimpleContacts-Info.plist and change the value for "Main nib file base name" to "Window".

这篇关于将UINavigationController添加到窗口时EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆