Reputation: 37
I am an iOS developer, trying to learn the differences between iOS and MacOS.
I have a very simple OSX app that runs fine on Yosemite but on Mavericks, the -viewDidLoad and other viewController methods are not being called, and I end up with an empty view.
I guess my main question is, what wasn't available in Mavericks and is in Yosemite that prevents such an elementary bit of code from operating? Is it possible that Mavericks doesn't support NSViewControllers the way Yosemite does?
Here is the code:
- (void)setupViewController
{
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.contentView = self.viewController.view;
self.viewController.view.frame = ((NSView*)self.window.contentView).bounds;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
if (self.viewController == nil)
[self setupViewController];
[self.window makeKeyAndOrderFront:self];
}
The app is setup with a MainMenu.xib which contains the app Window (and a view attached to that window which I replace above in code). There is also a ViewControler.xib which contains my main view.
And what can I do to have this application run on Mavericks?
UPDATE: It looks like NSViewController had no viewDidLoad, etc. prior to Yosemite. So, how best can I achieve something that works on both Mavericks and Yosemite?
Upvotes: 0
Views: 717
Reputation: 221
As Tom Andersen points out, awakeFromNib can be called multiple times. A cleaner solution would be to override loadView and call viewDidLoad yourself on Mavericks and earlier.
- (void)loadView
{
[super loadView];
if (!self.isOnYosemiteOrLater) {
[self viewDidLoad];
}
}
Upvotes: 0
Reputation: 2459
On 10.10, awakeFromNib
seems to be called more than once. It might be better to check OS version like below.
- (void)awakeFromNib {
if (![self respondsToSelector:@selector(viewWillAppear)]) {
// setup here on 10.9
....
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// setup here on 10.10
....
}
Upvotes: 2