我对Mac开发的可可很新。我目前正在努力将viewController的视图显示为NSBox的contentView。相关代码如下所示:
// 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;
}
目前,框中的contentView中没有显示任何内容。我该怎么做才能将viewControllers视图放入框中?
答案 0 :(得分:1)
问题是init太早,无法添加/更改视图。在AccountsViewController上实现awakeFromNib方法并在其中设置内容视图。在加载nib并准备好操作所有内容之后,将在您的类上调用awakeFromNib。