0

I just started to learn objective-C and am having issues with transitioning between loading a view (directly) to using a XIB as rootViewController. Currently I am trying to load my xib file into a view controller object. While it compiles without any problem, my simulator comes up blank (except for basic interface, time and battery fuel gauge). I also made sure to set my XIB into class BNRReminderViewController and set the view and respective button/objects. (I also imported my class BNRReminderViewController.h to my .m file)

Below is my code:

- (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;
}

I think what is happening is that my BNRReminderViewController.xib file is not within the mainBundle so when I init with the NSBundle object nothing loads which is why I am getting blank screen. I am new to objective-C so I do not really know how to deal with this as many other languages just import a .h or directly read the file. please help.

Norman Wei
  • 51
  • 5

2 Answers2

0

You can check if your hypothesis about the NSBundle is correct using rjstellings code here.

However, if you're using at least Xcode 5.0.1, you should be able to get away with not specifying the bundle name. Additionally, since your ViewController class appears to have the same name as the xib, Xcode is smart enough to let you get away with not needing to specify that either.

- (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;
}
Community
  • 1
  • 1
chriszumberge
  • 844
  • 2
  • 18
  • 33
0

You don't have to call mainBundle. It might be nil. This code might be solution:

    - (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; 
}

Otherwise, did you create app with Storyboard? If you start with Storyboard, you have to remove the line below: enter image description here

Gokhan Gultekin
  • 291
  • 5
  • 15