Search code examples
modal-dialognsview

NSView as a modal sheet


I am able to create a model sheet for an NSWindow and I used the following connection code.

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

I unable to create a modal for an NSView, Is it possible to create a modal sheet for NSView?


Solution

  • Sure it's possible but you have to embed it in newly created window. e.g.

      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];
    

    As you can see in documentation the acceptable argument for beginSheet is only window or its subclasses (e.g. 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");