将 UINavigationController 添加到窗口时 EXC_BAD_ACCESS

发布于 2024-11-10 06:07:22 字数 2158 浏览 0 评论 0原文

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

- (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

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

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;

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮光之海 2024-11-17 06:07:22

首先,您声明一个属性,但通过其实例变量而不是使用该属性来访问 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 并将“Main nib 文件基本名称”的值更改为“Window”。

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

Use this:

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

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文