程序关闭后,点击dock图标恢复窗口

1.在遵守NSApplicationDelegate的XXXdelegate.h里设置窗口的outlet

   在XXXdelegate.m里添加

- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag
{
    if (flag) {
         return NO;
        }
    else
        {
        [window makeKeyAndOrderFront:self];
        return YES;
        }
}

2.连接outlet NSWindow *window和nib里的窗口,在Interface Builder中,将主窗口的”Release When Closed”选项取消掉。

3.设置NSApplication的委托为XXXdelegate,也就是file‘s ower的outlet delegate设置为XXXdelegate。




其他写法:

http://blog.cnrainbird.com/index.php/2012/10/23/osx_cheng_xu_dian_ji_dock_tu_biao_zhong_xin_dan_chu_chuang_kou/

- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
                    hasVisibleWindows:(BOOL)flag{
  if (!flag){
    //主窗口显示
    [NSApp activateIgnoringOtherApps:NO];
    [self.window makeKeyAndOrderFront:self];
  }
  return YES;
}


http://hintsforums.macworld.com/showthread.php?t=40039

- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
                                hasVisibleWindows:(BOOL)flag
{	
 if (flag) {
  return NO;
 } else {
  [mainWindow orderFront:self];
  return YES;
 }	
}

你可能感兴趣的:(Mac开发)