如何编写一个toggle方法来打开/关闭一个窗口?

时间:2013-12-02 14:54:00

标签: cocoa osx-mavericks nswindowcontroller nspanel

我的应用中有一个NSPanel窗口,我想通过工具栏上的按钮切换打开和关闭。这看起来像是一个相当基本的操作,实际上我在很多应用程序中都看到过(比如Inspector视图)。但是,我很难找到正确的方法来做到这一点。

我查看了performClose:和makeKeyAndOrderFront:方法,但我无法弄清楚如何使它们在我的方法中工作。基本上,我想要这样的东西

-(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 个答案:

答案 0 :(得分:0)

在这里回答我自己的问题。但得到了我想要的东西:

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

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

}