2011-12-20 68 views
0

如何修改以及通過編程插入avCocoa NSButton? (適用於Mac的不是iPhone)如何修改以編程方式插入Cocoa NSButton的類和文件?

我曾嘗試以下,但它不工作:(的東西不是一個結構或聯合申請成員「意見」)

- (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]; 
} 
+2

您還沒有初始化該按鈕。你已經調用了'alloc'而不是'initWithFrame:'。在對類調用'alloc'後,必須始終調用對象的初始化方法。 – 2011-12-20 00:23:26

回答

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];  
} 
相關問題