Please, could you give me examples of how to create the NSBox
instance runtime to place it inside the NSView
?
Asked
Active
Viewed 2,529 times
2
-
I’ve added an example that works with garbage collection enabled. Let me know if you want me to fix it so that, when compiled without garbage collection, it doesn’t leak. – Jun 03 '11 at 10:13
1 Answers
3
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];
-
I have compiled the code without garbage collection support and there are no leaks for all allocated objects are being released: [box removeFromSuperview]; [box release]; [textField release]; [boxContentView release]; Thank you very much! – Jun 03 '11 at 10:43