Search code examples
objective-cxcodemacosnsview

How to deactivate or hide a view in Xcode?


In a NSView I have a Container View and two NSButton's (see the picture). The NSButton's change the content in the Container View. 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?

image


Solution

  • 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. 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).