initWithNibName not loading?


#1

Love the book so far! Working through it in conjunction with the Standford class online, and this book is definitely making sense of the things that were glossed over in the lectures.

I’m at a loss in the initial build of the Core Locations example… I’m using the simulator, but nothing ever happens… I tried to add an NSLog statement at the beginning of the initWithNibName, and it isn’t called. I even copied the code from the Solution, and initWithNibName still doesn’t seem to be firing.

I’m running Xcode 4.3.3 on Lion.

Any idea where I should be looking for a problem?

[code]
//.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface WhereamiViewController : UIViewController
{
CLLocationManager *locationManager;
}
@end[/code]

//.m
@implementation WhereamiViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self) {
        // Create location manager object
        locationManager = [[CLLocationManager alloc] init];
        
        // There will be a warning from this line of code; ignore it for now
        [locationManager setDelegate:self];
        
        // And we want it to be as accurate as possible
        // regardless of how much time/power it takes
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        // Tell our manager to start looking for its location immediately
        [locationManager startUpdatingLocation];
    }
    return self;
}
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"%@", newLocation);
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    NSLog(@"Could not find location: %@", error);
}

@end

#2

Start debugging from where you call the method - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil. Are you actually calling it?


#3

initWithNibName is never called in the code. The way I understood it, I wouldn’t need to explicitly call it, and it’s not in the instructions to add it anywhere.

I was able to make it work by switching it to awakeFromNib, or by changing it to init and calling init from viewDidLoad. I don’t know that I fully understand everything, but I’m wondering if initWithNibName is no longer implicitly called since Xcode 4.3.3 uses storyboards rather than nibs?


#4

I am not sure about the story boards.
However, since WhereamiViewController is a view controller, it must be loaded by someone:

...
WhereamiViewController *viewController =
                [[WhereamiViewController alloc] initWithNibName:@"WhereamiViewController" bundle:nil];
...

Or:

...
WhereamiViewController *viewController = [[WhereamiViewController alloc] init];
...

The last method requires that the init method be defined:

@implementation WhereamiViewController

...
- (id)init
{
      // call the designated init
      return [[[self class] alloc] initWithNibName:@"WhereamiViewController" bundle:nil];
}

...

#5

This is not exactly true. Yes, you can use storyboards, however Xcode 4.3.3 does not use storyboards rather than nibs. When you create a new project, it is you the developer who chooses whether or not to use storyboards for that project.

Also you say:

we call it in the AppDelegate for this project:

self.viewController = [[WhereAmIViewController alloc] initWithNibName:@"WhereAmIViewController" bundle:nil]; self.window.rootViewController = self.viewController;

Nick
http://myfirstiphoneapp.co.uk
http://easyintervalsapp.co.uk


#6

I’m definitely getting confused with nibs vs Storyboards. The way I understand it is since I chose storyboards, initWithNibName is not called (I looked through the AppDelegate, and it’s not in there). Looks like I should probably recreate the project without storyboards.

Thanks!


#7

Just recreated it - I glossed over the page with the project creation details, and it didn’t even register in my head that storyboards were an option. Now it works like a charm!

Thanks to everyone who had patience with me! :slight_smile: