如何将 NSStackView 注入视图层次结构?
How to inject a NSStackView into the view hierarchy?
我有一个用 Objective-C 编写的 OSX 应用程序。
它在 NSWindow
中显示一些 NSView
,
问题是我无法修改它的代码。原始模型层次结构如下所示:
NSWindow
|---> original NSView
|---> (...)
我想按如下方式更改层次结构:
NSWindow
|---> NSStackView
|---> original NSView
| |---> (...)
|---> some additional NSView (say NSTextField)
如何使用 NSStackView
将原始 NSView
和附加的 NSView
并排显示?
我目前的做法或多或少是这样的(示例已简化):
- (void)createFirstView {
NSTextField *label1 = [NSTextField labelWithString:@"First view."];
[_window setContentView: label1];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// I cannot modify this procedure:
[self createFirstView];
// I can modify that:
NSTextField *label2 = [NSTextField labelWithString:@"Second view."];
NSView *firstView = [_window contentView];
[firstView removeFromSuperview];
NSStackView *st = [NSStackView stackViewWithViews:@[firstView, label2]];
[_window setContentView:st];
}
不幸的是 运行 之后的 NSWindow
此代码仅显示 "Second view" 标签:
[_window setContentView:st]
在旧内容视图上调用 removeFromSuperview
,然后 removeFromSuperview
释放视图。 [firstView removeFromSuperview]
和 [_window setContentView:st]
都会释放 firstView
。
解决方案:将[firstView removeFromSuperview]
替换为[_window setContentView:nil]
。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// I cannot modify this procedure:
[self createFirstView];
// I can modify that:
NSTextField *label2 = [NSTextField labelWithString:@"Second view."];
NSView *firstView = [_window contentView];
[_window setContentView:nil];
NSStackView *st = [NSStackView stackViewWithViews:@[firstView, label2]];
[_window setContentView:st];
}
我有一个用 Objective-C 编写的 OSX 应用程序。
它在 NSWindow
中显示一些 NSView
,
问题是我无法修改它的代码。原始模型层次结构如下所示:
NSWindow
|---> original NSView
|---> (...)
我想按如下方式更改层次结构:
NSWindow
|---> NSStackView
|---> original NSView
| |---> (...)
|---> some additional NSView (say NSTextField)
如何使用 NSStackView
将原始 NSView
和附加的 NSView
并排显示?
我目前的做法或多或少是这样的(示例已简化):
- (void)createFirstView {
NSTextField *label1 = [NSTextField labelWithString:@"First view."];
[_window setContentView: label1];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// I cannot modify this procedure:
[self createFirstView];
// I can modify that:
NSTextField *label2 = [NSTextField labelWithString:@"Second view."];
NSView *firstView = [_window contentView];
[firstView removeFromSuperview];
NSStackView *st = [NSStackView stackViewWithViews:@[firstView, label2]];
[_window setContentView:st];
}
不幸的是 运行 之后的 NSWindow
此代码仅显示 "Second view" 标签:
[_window setContentView:st]
在旧内容视图上调用 removeFromSuperview
,然后 removeFromSuperview
释放视图。 [firstView removeFromSuperview]
和 [_window setContentView:st]
都会释放 firstView
。
解决方案:将[firstView removeFromSuperview]
替换为[_window setContentView:nil]
。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// I cannot modify this procedure:
[self createFirstView];
// I can modify that:
NSTextField *label2 = [NSTextField labelWithString:@"Second view."];
NSView *firstView = [_window contentView];
[_window setContentView:nil];
NSStackView *st = [NSStackView stackViewWithViews:@[firstView, label2]];
[_window setContentView:st];
}