我是COCOA的新手。我创建了一个cocoa应用程序来在视图中显示一个按钮。
CustomButton.h
#import <Cocoa/Cocoa.h>
@interface CustomButton : NSButton
{
}
@end
CustomButton .mm
#import "CustomButton.h"
@implementation CustomButton
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
[self setWantsLayer:YES];
[self.layer setBackgroundColor:[NSColor redColor].CGColor];
}
-(id) initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self)
{
CGSize objSize;
objSize.height = 50;
objSize.width = 100;
self.frame.size = objSize;
CGPoint objPoint;
objPoint.x = 10;
objPoint.y = 10;
self.frame.origin = objPoint;
self.stringValue = @"Test Button";
}
return self;
}
@end
MainWindow.h是窗口控制器类。
#import <Cocoa/Cocoa.h>
#import "CustomButton.h"
@interface MainWindow : NSWindowController
{
IBOutlet NSView *m_pViewButton1;
CustomButton* m_pButton1;
}
@end
在mainWindow.mm旁边
- (void)windowDidLoad {
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
NSRect objRect;
objRect.origin = m_pViewButton1.frame.origin;
objRect.size = m_pViewButton1.frame.size;
[m_pViewButton1 setWantsLayer:YES];
[m_pViewButton1.layer setBackgroundColor:[NSColor greenColor].CGColor];
m_pButton1 = [[CustomButton alloc] initWithFrame:objRect];
[m_pViewButton1 addSubview:m_pButton1];
}
但在运行应用程序时,不会显示按钮。
我不知道缺少哪种方法调用。
请帮助。 谢谢大家。
此致 菲尔