简体   繁体   中英

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?

图片

I'm not sure if it's faster, but it's certainly more convenient to use a tabless 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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM