Xib 未按预期加载

Xib doesn't load as expected

我已经有一段时间没有从头开始创建项目了。当时在 XCode 4 / 5 时,开发人员可以选择故事板或 Xib 来启动项目。在 Xcode 6 中,虽然每个模板都被强制作为 StoryBoard 启动。

我已经删除了故事板。然后我在支持文件中打开 info.plist 并将 Main storyboard file base name : Main 设置为 Main storyboard file base name: <blank>

然后我用 Xib(同名)创建了一个基本的 testViewController。

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    testViewController *firstViewController = [[testViewController alloc] initWithNibName:nil bundle:nil];
    self.navigationController.viewControllers = [NSArray arrayWithObject:firstViewController];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

这仍然向我展示了一个黑色视图控制器。我检查了 xib 文件,文件所有者设置为视图控制器并且还设置了视图委托。 请问我错过了什么?

改变这两件事:

 @property (nonatomic, retain) IBOutlet UIWindow *window;

 testViewController *firstViewController = [[testViewController alloc] initWithNibName:nil bundle:nil];

为此,添加此代码,使用真实姓名或您的 xib 文件:

@property (nonatomic, retain) UIWindow *window;  // Are you using ARC?

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// I think your xib files is testViewcontroller.xib, if not change it.
testViewController *firstViewController = [[testViewController alloc] initWithNibName:testViewController.xib bundle:nil];
// change this also
self.navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];

self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;