如何以及以什么类/文件来修改以编程方式插入Cocoa NSButton?

时间:2011-12-20 00:03:33

标签: objective-c cocoa

如何修改类和文件以便以编程方式插入avCocoa NSButton? (对于Mac而不是iPhone)

我已经尝试了以下但是它不起作用:(在非结构或联合的东西中请求成员“视图”。)

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
    NSButton *btn = [NSButton alloc];
    [self.view addSubview:btn];
}

1 个答案:

答案 0 :(得分:1)

您必须将其添加到窗口的内容视图中。

- (void)windowControllerDidLoadNib:(NSWindowController *)aController {
    [super windowControllerDidLoadNib:aController];

    // Get our content view.
    NSView *contentView = aController.window.contentView;

    // Create the rectangle in which to place the button.
    NSRect buttonFrame = NSMakeRect(0, 0, 100, 25);
    buttonFrame.origin.x = round((contentView.frame.size.width-buttonFrame.size.width)/2);
    buttonFrame.origin.y = round((contentView.frame.size.height buttonFrame.size.height)/2);

    // Create and add the button.
    NSButton *button = [[[NSButton alloc] initWithFrame:buttonFrame] autorelease];
    [button setBezelStyle:NSRoundedBezelStyle];
    [contentView addSubview:button];    
}