使用ARC在Lion 10.7上以编程方式创建和打开NSWindow [英] Programmatically create and open an NSWindow with ARC on Lion 10.7
问题描述
在google上搜索2小时后,我不知道如何分配和打开一个没有nib的新NSWindow。
After 2 hours of searching on google and so, i can't figure out how to allocate and open a new NSWindow without nib.
NSRect frame = NSMakeRect(100, 100, 200, 200);
NSUInteger styleMask = NSBorderlessWindowMask;
NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];
NSWindow * window = [[NSWindow alloc] initWithContentRect:rect styleMask:styleMask backing: NSBackingStoreBuffered defer:false];
[window setBackgroundColor:[NSColor blueColor]];
[window makeKeyAndOrderFront: window];
上面的代码来自此线程如何以编程方式创建Cocoa窗口?
我会疯了:(
The code above is taken from this thread How do I create a Cocoa window programmatically? I am going crazy :(
推荐答案
如果你使用ARC,除非你有一个强大的窗口引用,它将被引用它的最后一个语句后立即释放。
If you're using ARC, then unless you have a strong reference to the window, it will be released immediately after the last statement that references it.
ARC改变了你需要考虑对象的方式,从一个retain / release模型到一个所有权模型如果没有你的 code>变量,它会消失。
ARC changes the way you need to think about objects, from a retain/release model to an ownership model. If nothing owns your window
variable, it will go away.
有几种方法来获取窗口的所有权。可以将窗口设置为实例变量或您的类中的属性使用 strong
关键字,或者您可以使用 __ strong
There are several ways to take ownership of the window. You can set the window as either an instance variable or as a property in your class using the strong
keyword, or you can use the __strong
qualifier when you declare the variable in your code.
有关ARC的更多信息,请参见 LLVM编译器网站。
There is a lot more information about ARC at the LLVM compiler site.
这篇关于使用ARC在Lion 10.7上以编程方式创建和打开NSWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!