问题描述:

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.

网友答案:

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

相关阅读:
Top