objective-c - 如何将 NSStackView 注入视图层次结构?
问题描述
我有一个用 Objective-C 编写的 OSX 应用程序。它显示了一些NSView
,NSWindow
问题是我无法修改它的代码。原始模型层次结构如下所示:
NSWindow
|---> original NSView
|---> (...)
我想改变层次结构如下:
NSWindow
|---> NSStackView
|---> original NSView
| |---> (...)
|---> some additional NSView (say NSTextField)
如何使用?NSView
_NSView
NSStackView
我目前的方法或多或少是这样的(示例已简化):
- (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
,运行此代码后仅显示“第二视图”标签:
解决方案
[_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];
}
推荐阅读
- javascript - 所有圆圈计数器显示相同的数据(如何让每个圆圈显示它自己的计数器数据值)
- c# - 如何在 HTML 的 HiddenField 值中添加新行?
- sql-server - 如何在excel工作簿中重置现有的sql server连接
- java - 在 SpringBoot 中使用嵌入式存根/虚拟应用程序进行测试
- amazon-web-services - 有没有办法将 CloudWatch 指标流式传输到 Lambda?
- javascript - 如何对集合中的多个值进行比较?
- azure - 如何从 Azure 区块链即服务中提取仲裁日志文件
- android - 如何提高将重图像上传到服务器的性能
- python-3.x - “uvloop”没有属性“EventLoopPolicy”
- docker - 无法在节点 docker 映像中安装 npm