xcode Objective-c:单击按钮时将视图添加到窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4416860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
objective-c: add view to window on button click
提问by PruitIgoe
I'm new to objective-c so bear with me. This seems pretty simple but I must be doing something wrong. In IB I created an object, called it AppController and then added an action titled makeView and two IBOutlets, btnMakeView and mainWin, then connected everything properly and wrote the class file from that.
我是objective-c的新手,所以请耐心等待。这看起来很简单,但我一定是做错了什么。在 IB 中,我创建了一个对象,将其命名为 AppController,然后添加了一个名为 makeView 的操作和两个 IBOutlets、btnMakeView 和 mainWin,然后正确连接所有内容并从中编写类文件。
In AppController.h I have this code:
在 AppController.h 我有这个代码:
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject {
IBOutlet NSWindow * mainWin;
IBOutlet id btnMakeView;
}
- (IBAction)makeView:(id)sender;
@end
in AppController.m this code:
在 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
in ViewController.h I have this code:
在 ViewController.h 我有这个代码:
#import <Cocoa/Cocoa.h>
@interface ViewController : NSView {
}
@end
and finally in ViewController.m I have this code:
最后在 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
It compiles fine, there are no errors but do not get an object, which I am assuming will be a rectangle, starting at x,y 10/10 and being 100x20. What am I doing wrong?
它编译得很好,没有错误但没有得到一个对象,我假设它是一个矩形,从 x,y 10/10 开始,为 100x20。我究竟做错了什么?
回答by outis
Does mainWin
refer to a valid window? Other than that, your code works, though it could use some improvement. For example,
是否mainWin
引用有效窗口?除此之外,您的代码可以工作,但它可以使用一些改进。例如,
ViewController
is a view, not a controller, so it shouldn't be named a controller.- You've got a memory leak. You never release the
ViewController
you create. The memory management rules are very simple; read up on them.
ViewController
是视图,而不是控制器,因此不应将其命名为控制器。- 你有内存泄漏。你永远不会释放
ViewController
你创造的。该内存管理规则非常简单; 仔细阅读它们。
Try the following changes. First, rename ViewController
to RedView
(or SolidColorView
or somesuch). You can do this very simply by refactoring: right-clickthe class name in the @interface
or @implementation
declaration and choose "Refactor...".
尝试以下更改。首先,重命名ViewController
为RedView
(SolidColorView
或诸如此类)。您可以通过重构非常简单地做到这一点:右键单击@interface
or@implementation
声明中的类名,然后选择“重构...”。
In RedView.m:
在 RedView.m 中:
- (id)initWithFrame:(NSRect)frame {
// the idiom is to call super's init method and test self all in the
// same line. Note the extra parentheses around the expression. This is a
// hint that we really want the assignment and not an equality comparison.
if ((self = [super initWithFrame:frame])) {
// Initialization code here.
}
return self;
}
In AppController.m:
在 AppController.m 中:
- (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.
RedView* testbox = [[RedView alloc] initWithFrame:NSMakeRect(10.0, 10.0, 100.0, 20.0)];
//this tells the window to add our subview testbox to it's contentview
// You don't need to keep a reference to the main window. The application already
// does this. You might forget to hook up mainWin, but the following won't fail
// as long as the app has a main window.
[[[[NSApplication sharedApplication] mainWindow] contentView] addSubview:testbox];
// We're done with testbox, so release it.
[testbox release];
}