In the didFinishLaunchingWithOptions method in the App Delegate, a string, myString is initialised with a string retrieved from NSUserDefaults (confirmed with an NSLog). I have multiple view controllers in this tab-based project, and all of them are accessing the app delegate variables just fine, apart from one. The only difference between this view controller and the others is that it is a UIViewController as opposed to a UITableViewController (though the latter inherited).
In the UIViewController‘s initialisation I’ve put:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (self.appDelegate.myString == nil)
NSLog(@"nil");
}
return self;
}
The strange part: in the initWithNibName method, myString (and all other variables in the app delegate) is nil. However when I fire this in the viewDidAppear method, it isn’t nil (and I get the correct string). Furthermore it returns nil in the viewDidLoad method too. So I tried initialising self.appDelegate in viewDidLoad and then checking the string but it still returns nil.
I’ve gone wrong somewhere but can’t figure it out.
Just an oversight. I initialised the view controller before retrieving the variable from
NSUserDefaults.