我正处于学习编程/ Cocoa的早期阶段,我决定以编程方式创建一个简单的UI,以便更好地可视化代码/ GUI之间的关系。我在OS X中打开了Cocoa的默认模板。到目前为止,我对模板的唯一修改是:
@property NSButton *theButton;
在Appdelegate.h中 和
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
CGRect buttonFrame;
buttonFrame = CGRectMake(20, 20, 15, 5);
self.theButton = [[NSButton alloc]initWithFrame:buttonFrame];
[self.theButton setTitle:@"test"];
[self.window addSubview:self.theButton];
}
在Appdelegate.m。
但是我收到了错误:
[self.window addSubview:self.theButton];
'NSWindow'没有可见的@interface声明选择器'addSubview:'
为什么会这样?
答案 0 :(得分:4)
NSWindow继承自NSResponder,因此您可以使用类似
[self.window.contentView addSubview: yourbutton];
但是对于UIWindow你可以使用。 [self.window addSubview:button]
因为它继承自UIView。希望它会更清楚......
答案 1 :(得分:1)
因为NSWindow没有addSubview:方法。该方法是NSView的一部分。您可能想要窗口的contentView。