how to write a toggle method to open/close a window?

146 views Asked by At

I have an NSPanel window in my app that I want to toggle open and close with a button on the toolbar. This seems like a fairly basic operation, and indeed one I see in many apps (like Inspector views). However, I'm struggling to find the right way to do this.

I've looked at the performClose: and the makeKeyAndOrderFront: methods, but I can't work out how to make them work in my method. Basically, I want something like this

-(IBAction)togglePanel:(id)sender {  

if  (   ) //what do i put here to assess if _myPanel is already open?  

    // tell _myPanel to close  

else {  

        //tell _myPanel to open  

    }
  }
1

There are 1 answers

0
applehelpwriter On

Answering my own question, here. But got what I wanted thus:

- (IBAction)togglePanel:(id)sender {
if (_myPanel.isVisible == 0)

    [_myPanel makeKeyAndOrderFront:self];
else {
    [_myPanel performClose:self];
      }

}