objective-c - 将 UINavigationController 添加到窗口时出现 EXC_BAD_ACCESS

标签 objective-c cocoa-touch ios uiviewcontroller uinavigationcontroller

我正在开发一个使用多个 View Controller 和导航 Controller 的应用程序。当应用程序运行并执行以下代码时,它会在尝试添加 subview 时抛出异常。

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

我这样声明导航 Controller :

@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;

我已将导航 Controller 连接到 XIB 中的委托(delegate)对象。请随时查看我的完整来源:https://github.com/agmcleod/SimpleContacts/tree/parttwo

我试过将 Instruments 与 NSZombie 一起使用,但它似乎并没有停下来让我检查具体出了什么问题。它有时也会继续运行,不会终止。我最终不得不使用事件终端强制退出它。

最佳答案

首先,您声明一个属性,但通过其实例变量而不是使用该属性访问 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 file base name”的值更改为“Window”。

关于objective-c - 将 UINavigationController 添加到窗口时出现 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6170921/

相关文章:

ios - TableView 的一种方法,仅在第一次显示 View 时调用

ios - 使用 UIDocumentPickerViewController,是否可以像在 Slack 中一样在首次打开时显示默认服务(Dropbox、Google Drive 等)?

objective-c - 为 UITableView 的每个部分添加单独的标题

ios - UIAlertView 不打开 url?

swift - DidSelectRowAtIndexPath 根本没有被调用

c# - 在 UITableView 的错误单元格中出现 UISwitch

javascript - HTML5 拖放 - 不适用于 iOS 12.1.2(Safari 和 Chrome)

ios - 通过initWithFrame:CGRectMake()传递另一个参数

ios - 外部附件 : Receive Data using USB cable

ios - 如何存储在线图像和数据以供离线使用