NSView 作为模式 sheet

NSView as a modal sheet

我能够为 NSWindow 创建模型 sheet,并且我使用了以下连接代码。

[NSApp beginSheet:self.view modalForWindow:[[NSApplication sharedApplication] mainWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];

我无法为 NSView 创建模态,是否可以为 NSView 创建模态 sheet?

当然可以,但您必须将其嵌入到新创建的 window 中。 例如

  NSView *view = [[NSView alloc] initWithFrame:NSMakeRect(100, 100, 300, 300)];
  self.windowSheet = [[NSWindow alloc] initWithContentRect:[view frame] styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:YES];
  self.windowSheet.contentView = view;
  [NSApp beginSheet:self.windowSheet modalForWindow:[[NSApplication sharedApplication] mainWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];

正如您在文档中看到的,beginSheet 的可接受参数仅为 window 或其子类(例如 NSPanel)

- (void)beginSheet:(NSWindow *)sheet modalForWindow:(NSWindow *)docWindow modalDelegate:(nullable id)modalDelegate didEndSelector:(nullable SEL)didEndSelector contextInfo:(null_unspecified void *)contextInfo NS_DEPRECATED_MAC(10_0, 10_10, "Use -[NSWindow beginSheet:completionHandler:] instead");