我正在开发一个使用多个 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/