Objective c Can';看不到新的子视图

Objective c Can';看不到新的子视图,objective-c,cocoa,nsview,Objective C,Cocoa,Nsview,我所要做的就是在NSWindow实例的内容视图中添加一个新视图。当我执行以下操作时,我看不到新视图(它应该是黑色的,占据整个窗口)。我做错了什么 (响应按钮点击完成) 我在ViewController中设置了一个简单的项目,其中有一个按钮,并在访问self.window时收到警告。使用self.view.window时,警告消失,您提供的代码正常工作 @implementation WindowController - (void)windowDidLoad { [super wind

我所要做的就是在NSWindow实例的内容视图中添加一个新视图。当我执行以下操作时,我看不到新视图(它应该是黑色的,占据整个窗口)。我做错了什么

(响应按钮点击完成)


我在ViewController中设置了一个简单的项目,其中有一个按钮,并在访问
self.window
时收到警告。使用
self.view.window
时,警告消失,您提供的代码正常工作

@implementation WindowController

- (void)windowDidLoad
{
    [super windowDidLoad];

    CGRect buttonRect = CGRectMake(self.window.frame.size.width / 2 - 50,
                                   self.window.frame.size.height / 2,
                                   100,
                                   50);
    NSButton *button = [[NSButton alloc] initWithFrame:NSRectFromCGRect(buttonRect)];
    [button setTitle: @"Click me!"];
    [button setTarget:self];
    [button setAction:@selector(buttonPressed)];
    [self.window.contentView addSubview:button];
}

- (void)buttonPressed
{
    NSRect frameRect = [self.window frame];
    frameRect.origin = NSZeroPoint;

    NSView *view = [[NSView alloc] initWithFrame:frameRect];
    view.wantsLayer = YES;
    view.layer.backgroundColor = [NSColor blackColor].CGColor;

    [self.window.contentView addSubview:view];
}
更新代码

更新 假设您使用的是WindowController实例,其中以编程方式添加了一个按钮,那么代码将按预期工作

@implementation WindowController

- (void)windowDidLoad
{
    [super windowDidLoad];

    CGRect buttonRect = CGRectMake(self.window.frame.size.width / 2 - 50,
                                   self.window.frame.size.height / 2,
                                   100,
                                   50);
    NSButton *button = [[NSButton alloc] initWithFrame:NSRectFromCGRect(buttonRect)];
    [button setTitle: @"Click me!"];
    [button setTarget:self];
    [button setAction:@selector(buttonPressed)];
    [self.window.contentView addSubview:button];
}

- (void)buttonPressed
{
    NSRect frameRect = [self.window frame];
    frameRect.origin = NSZeroPoint;

    NSView *view = [[NSView alloc] initWithFrame:frameRect];
    view.wantsLayer = YES;
    view.layer.backgroundColor = [NSColor blackColor].CGColor;

    [self.window.contentView addSubview:view];
}

NSViewController
的实例没有
window
的属性-只有
NSWindowController
有一个属性

谢谢@flashfabrixx。我也可以用VC来实现这一点,但我正在试图理解为什么直接访问self.window.contentView时它不起作用。您的代码可能正在添加对象,但是,如果你不使用控制器,该怎么更新用户界面呢?@RobertJoseph更新了我的答案。如果你调用setNeedsDisplay,可能会发生什么?如果你去掉图层内容会发生什么?并使用自定义的
NSView
和一些虚拟的drawRect方法(例如,用一些随机颜色填充整个帧)
@implementation WindowController

- (void)windowDidLoad
{
    [super windowDidLoad];

    CGRect buttonRect = CGRectMake(self.window.frame.size.width / 2 - 50,
                                   self.window.frame.size.height / 2,
                                   100,
                                   50);
    NSButton *button = [[NSButton alloc] initWithFrame:NSRectFromCGRect(buttonRect)];
    [button setTitle: @"Click me!"];
    [button setTarget:self];
    [button setAction:@selector(buttonPressed)];
    [self.window.contentView addSubview:button];
}

- (void)buttonPressed
{
    NSRect frameRect = [self.window frame];
    frameRect.origin = NSZeroPoint;

    NSView *view = [[NSView alloc] initWithFrame:frameRect];
    view.wantsLayer = YES;
    view.layer.backgroundColor = [NSColor blackColor].CGColor;

    [self.window.contentView addSubview:view];
}