Pregunta

I'm pretty new to cocoa for Mac development. I'm currently struggling with getting a viewController's view to show up as a NSBox's contentView. The relevant code looks like this:

// AccountsViewController.h. ManagingViewController is a custom subclass of NSViewController
// as of Cocoa Programming for Mac.
@interface AccountsViewController : ManagingViewController
{
    LoginViewController *loginViewController;
}

@property (strong) IBOutlet NSBox *box; 

// Implementation

@implementation AccountsViewController
@synthesize box;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.

        // Should display the view in the contentView(!?!)
        loginViewController = [[LoginViewController alloc]   initWithNibName:@"LoginViewController" bundle:nil];
        box.contentView = loginViewController.view;
    }

    return self;
}

Currently, nothing displays in the box's contentView. What should I do to get the viewControllers view into the box?

¿Fue útil?

Solución

The problem is that init is too early to be adding/changing views. Implement an awakeFromNib method on the AccountsViewController and set the content view there. awakeFromNib will be called on your class after the nib is loaded and everything is ready to be manipulated.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top