Floeckchen Hemmy
Floeckchen Hemmy

Reputation: 21

Load custom viewcontroller from xib

Hello I have an OS X application with multiple windows and views. Now i wanted to refactor some code and tried to load my custom viewcontroller through my generated xib file with follong method:

TestViewController* tableViewController =
[[TestViewController alloc] initWithNibName:@"TestView" bundle:nil];

TestViewController is a subclass of NSViewController. At this point everything is fine, (i hope) because i see no exception.

To get a bit more understanding of my code, here is the part that throws the error:

        if (tableViewController != nil)
        {
            myCurrentViewController = tableViewController;
            [myCurrentViewController setTitle:@"TestView"];
        }

        [[self window] setContentSize:[smallView frame].size];
        [[[self window] contentView] addSubview:[myCurrentViewController view]]; //here is the error

I already tried things like check for files owner, main interface is MainMenu ...

Upvotes: 1

Views: 2389

Answers (1)

Floeckchen Hemmy
Floeckchen Hemmy

Reputation: 21

Finally I found the solution for my problem.

My ViewController with Xib was generated by XCode and all files have the same name schema like TestViewController.h, TestViewController.m and TestViewController.xib.

All the examples i found reference the xib file with "TestView", so i thought xcode matches the correct viewcontroller with its view by adding the "Controller" ending to the xib name.

In my case i had to rename my xib file from TestViewController.xib to TestView.xib and i could load it with @"TestView".

Hth

Upvotes: 1

Related Questions