将 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 中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您声明一个属性,但通过其实例变量而不是使用该属性来访问 UINavigationController。
使用这个:
其次,您将主 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:
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".