【问题标题】:Create NSBox runtime example创建 NSBox 运行时示例
【发布时间】:2011-06-03 09:07:12
【问题描述】:

请问,您能否举例说明如何创建 NSBox 实例运行时以将其放置在 NSView 中?

【问题讨论】:

  • 我添加了一个启用垃圾收集的示例。如果您希望我修复它,请告诉我,以便在没有垃圾收集的情况下编译时不会泄漏。

标签: objective-c cocoa nsbox


【解决方案1】:
NSView *superview = …; // Reference to the view that will contain the box
                       // for instance, [window contentView]

NSUInteger resizeAllMask = (NSViewWidthSizable
    | NSViewHeightSizable
    | NSViewMinXMargin
    | NSViewMaxXMargin
    | NSViewMinYMargin
    | NSViewMaxYMargin);

// This is the box. We use an autoresizing mask so that it occupies
// the entire superview 
NSBox *box = [[NSBox alloc] initWithFrame:[superview bounds]];
[box setAutoresizingMask:resizeAllMask];
[box setBoxType:NSBoxPrimary];
[box setBorderType:NSBezelBorder];
[box setTitle:@"This is a box"];

// This is the box' content view. It represents the box contents.
// By default, a box autoresizes its content view so that it occupies
// the entire box
NSView *boxContentView = [[NSView alloc] initWithFrame:NSZeroRect];
[box setContentView:boxContentView];

// For example, we add a text field to the box' content view
NSRect textRect = {{0,0}, {100,20}};
NSTextField *textField = [[NSTextField alloc] initWithFrame:textRect];
[textField setStringValue:@"Hey there"];
[boxContentView addSubview:textField];

[superview addSubview:box];

【讨论】:

  • 我编译了没有垃圾收集支持的代码,并且所有分配的对象都没有泄漏:[box removeFromSuperview]; [盒子释放]; [文本字段发布]; [boxContentView 发布];非常感谢!
猜你喜欢
  • 1970-01-01
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多