Я новичок в объективе-c, так что терпите меня.Это кажется довольно простым, но я, должно быть, делаю что-то не так.В IB я создал объект под названием AppController, а затем добавил действие под названием makeView и два IBOutlets, btnMakeView и mainWin, затем правильно соединил все и написал файл класса из этого.
В AppController.h у меня есть этот код:
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject {
IBOutlet NSWindow * mainWin;
IBOutlet id btnMakeView;
}
- (IBAction)makeView:(id)sender;
@end
в AppController.m этот код:
#import "AppController.h"
#import "ViewController.h"
@implementation AppController
- (IBAction)makeView:(id)sender {
//declare a new instance of viewcontroller, which inherits fromNSView, were going to allocate it with
//a frame and set the bounds of that frame using NSMakeRect and the floats we plug in.
ViewController* testbox = [[ViewController alloc]initWithFrame:NSMakeRect(10.0, 10.0, 100.0, 20.0)];
//this tells the window to add our subview testbox to it's contentview
[[mainWin contentView]addSubview:testbox];
}
-(BOOL)isFlipped {
return YES;
}
@end
в ViewController.h У меня есть этот код:
#import <Cocoa/Cocoa.h>
@interface ViewController : NSView {
}
@end
и, наконец, в ViewController.m У меня есть этот код:
#import "ViewController.h"
@implementation ViewController
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)rect {
// Drawing code here.
//sets the background color of the view object
[[NSColor redColor]set];
//fills the bounds of the view object with the background color
NSRectFill([self bounds]);
}
@end
Он компилируется нормально, нет ошибок, но не получаю объект, который я предполагаю, будетпрямоугольник, начиная с x, y 10/10 и 100x20.Что я делаю не так?