[英]How to deactivate or hide a view in Xcode?
In a NSView I have a Container View and two NSButton's (see the picture).在 NSView 中,我有一个容器视图和两个 NSButton(见图)。 The NSButton's change the content in the Container View.
NSButton 更改容器视图中的内容。 To show a view and hide the other one, I do this:
为了显示一个视图并隐藏另一个视图,我这样做:
@IBAction func changeView(sender: NSButton)
{
switch sender.id
{
case "Button 1" { view1.hidden = true; view2.hidden = false; }
case "Button 2" { view1.hidden = false; view2.hidden = true; }
}
}
It works properly, BUT since in the views there's a lot to draw (fields, buttons and images), switching from a view to another is a little bit (very little) slow.它工作正常,但是因为在视图中有很多要绘制(字段、按钮和图像),从一个视图切换到另一个视图有点(非常少)慢。 And I have the doubt that this method is not the correct one.
而且我怀疑这种方法不是正确的方法。
How could I switch from a view to another in a proper way?我怎样才能以适当的方式从一个视图切换到另一个视图?
I'm not sure if it's faster, but it's certainly more convenient to use a tabless NSTabView
.我不确定它是否更快,但使用表
NSTabView
肯定更方便。 That way, you can set up the views in a nib without the confusion of views that seem to overlap.这样,您可以在笔尖中设置视图,而不会混淆似乎重叠的视图。
Hiding should be fine.隐藏应该没问题。 You might want to switch the order so that you always set a view hidden before setting the other view unhidden.
您可能希望切换顺序,以便在将其他视图设置为隐藏之前始终将视图设置为隐藏。
You could also remove a view from the view hierarchy ( removeFromSuperview()
) as a way to effectively hide it.您还可以从视图层次结构 (
removeFromSuperview()
) 中删除视图作为有效隐藏它的一种方式。 Be sure that something in your code maintains a strong reference to the view.确保代码中的某些内容保持对视图的强引用。 The view controller does, so that's presumably good enough (assuming you also have a strong reference to the view controller).
视图控制器可以,所以这可能已经足够好了(假设您也有对视图控制器的强引用)。
And, yes, putting the views in an NSTabView
and having that switch them also works (basically by using one of the above techniques).而且,是的,将视图放在
NSTabView
并进行切换也可以(基本上通过使用上述技术之一)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.