0

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?

Mel Burr
  • 37
  • 6

2 Answers2

2

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
    ....
}
bluedome
  • 2,449
  • 1
  • 22
  • 17
  • 1
    Note: Calling makeViewWithIdentifier:owner: causes awakeFromNib to be called multiple times in your app. This is because makeViewWithIdentifier:owner: loads a NIB with the passed-in owner, and the owner also receives an awakeFromNib call, even though it’s already awake. -- From Apple. https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TableView/PopulatingView-TablesProgrammatically/PopulatingView-TablesProgrammatically.html – Tom Andersen May 29 '15 at 15:27
  • So I use a simple flag in awake from nib to call one off init on 10.9 – Tom Andersen May 29 '15 at 15:28
  • If you place a `NSViewController` as an object into a NIB file and tell it to load another NIB file that contains the view it will control (and where it is the owner of that NIB file), then `awakeFromNib` is also called twice. It is called once when the view controller itself has been loaded of its NIB file (so you can make programmatic changes to it) and it is called again after the view controller has loaded its view from that other NIB file, as only then the `IBOutlets` will be set in case you want to initialize those as well. – Mecki May 29 '19 at 16:44
0

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];
    }
}
Steve Shepard
  • 221
  • 2
  • 5