加载的Xib文件,但没有出现
问题描述:
我刚开始学习Objective-C,并且在加载视图(直接)到使用XIB作为rootViewController之间进行转换时遇到问题。目前我正在尝试将我的xib文件加载到视图控制器对象中。虽然它编译没有任何问题,我的模拟器空白(除基本接口,时间和电池电量计)。我还确保将我的XIB设置为类BNRReminderViewController并设置视图和相应的按钮/对象。 (我还进口了我的课BNRReminderViewController.h我.m文件)加载的Xib文件,但没有出现
下面是我的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
NSBundle *appBundle = [NSBundle mainBundle];
BNRReminderViewController *rvc = [[BNRReminderViewController alloc] initWithNibName:@"BNRReminderViewController" bundle:appBundle];
self.window.rootViewController = rvc;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
我认为正在发生的事情是,我BNRReminderViewController.xib文件不是mainBundle内,所以当我初始化NSBundle对象没有加载,这就是为什么我得到空白屏幕。我是Objective-C的新手,所以我不知道如何处理这个问题,因为许多其他语言只是导入.h或直接读取文件。请帮忙。
答
你可以使用rjstellings代码here来检查你的关于NSBundle的假设是否正确。
但是,如果您至少使用Xcode 5.0.1,那么您应该能够避免指定包名称。此外,由于您的ViewController类出现与xib具有相同的名称,因此Xcode足够聪明,可以让您不必指定该名称。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
BNRReminderViewController *rvc = [[BNRReminderViewController alloc] init];
[self.window setRootViewController:rvc];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
答
您不必调用mainBundle。它可能是零。此代码可能是解决方案:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.dashboardVC = [[DashboardViewController alloc] initWithNibName:@"DashboardViewController" bundle:nil];
UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:self.dashboardVC];
self.window.rootViewController = nc;
[self.window makeKeyAndVisible];
return YES;
}
否则,您是否使用Storyboard创建应用程序?如果你从Storyboard开始,你必须删除下面的行:
= /似乎没有工作 – 2014-09-26 20:56:29
你试过rjstelling的代码来检查捆绑? – chriszumberge 2014-09-28 16:04:16