代码之家  ›  专栏  ›  技术社区  ›  Redwood

如何使用工作表的返回值来决定是否关闭窗口?

  •  0
  • Redwood  · 技术社区  · 16 年前

    我想用 windowShouldClose: 在我的nswindowcontroller子类中弹出一张工作表,询问用户是否要在使用“保存”、“取消”和“不保存”按钮关闭前保存更改。

    我遇到的问题是 beginSheetModalForWindow:... 使用委托而不是返回值。

    我可以退不进去 窗口应关闭: 但是当我发送 [self close] 对于面板代理中的控制器,不会发生任何事情。

    有人能给我解释一下怎么做吗,或者给我指出一些示例代码的方向吗?

    2 回复  |  直到 12 年前
        1
  •  2
  •   Konamiman    16 年前

    基本的解决方案是在窗口上放置一个布尔标志,该标志说明窗口是否警告过未保存的更改。在调用[自动关闭]之前,请将此标志设置为true。

    最后,在windowshouldclose方法中,返回标志的值。

        2
  •  2
  •   Redwood    16 年前

    这是我最后使用的代码。

    windowShouldCloseAfterSaveSheet_ 是我的控制器类中的实例变量。

    记住在ib中设置控制器的窗口出口。

    - (BOOL)windowShouldClose:(id)window {    
      if (windowShouldCloseAfterSaveSheet_) {
        // User has already gone through save sheet and choosen to close the window
        windowShouldCloseAfterSaveSheet_ = NO; // Reset value just in case
        return YES;
      }
    
      if ([properties_ settingsChanged]) {
        NSAlert *saveAlert = [[NSAlert alloc] init];
        [saveAlert addButtonWithTitle:@"OK"];
        [saveAlert addButtonWithTitle:@"Cancel"];
        [saveAlert addButtonWithTitle:@"Don't Save"];
        [saveAlert setMessageText:@"Save changes to preferences?"];
        [saveAlert setInformativeText:@"If you don't save the changes, they will be lost"];
        [saveAlert beginSheetModalForWindow:window
                                    modalDelegate:self
                                   didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) 
                                      contextInfo:nil];
    
        return NO;
      }
    
      // Settings haven't been changed.
      return YES;
    }
    
    // This is the method that gets called when a user selected a choice from the
    // do you want to save preferences sheet.
    - (void)alertDidEnd:(NSAlert *)alert 
             returnCode:(int)returnCode
            contextInfo:(void *)contextInfo {
      switch (returnCode) {
        case NSAlertFirstButtonReturn:
          // Save button
          if (![properties_ saveToFile]) {
            NSAlert *saveFailedAlert = [NSAlert alertWithMessageText:@"Save Failed"
                                                       defaultButton:@"OK"
                                                     alternateButton:nil
                                                         otherButton:nil
                                           informativeTextWithFormat:@"Failed to save preferences to disk"];
            [saveFailedAlert runModal];
          }
          [[alert window] orderOut:self];
          windowShouldCloseAfterSaveSheet_ = YES;
          [[self window] performClose:self];
          break;
        case NSAlertSecondButtonReturn:
          // Cancel button
          // Do nothing
          break;
        case NSAlertThirdButtonReturn:
          // Don't Save button
          [[alert window] orderOut:self];
          windowShouldCloseAfterSaveSheet_ = YES;
          [[self window] performClose:self];
          break;
        default:
          NSAssert1(NO, @"Unknown button return: %i", returnCode);
          break;
      }
    }
    
    推荐文章