2015-09-18 25 views
1

我能夠爲NSWindow創建模型表,並使用以下連接代碼。NSView作爲模態表

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

我無法爲NSView創建模態,是否可以爲NSView創建模態表?

回答

1

確定這是可能的,但您必須將其嵌入新創建的窗口中。 例如

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

正如你可以在文檔中看到beginSheet可接受的說法是唯一窗口或它的子類(如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"); 
+0

謝謝您的答覆,但我不希望裏面嵌入我的NSView一個窗口,是否有可能不嵌入NSWindow。 – Barath

+0

不可以。唯一可以接受的論點是NSWindow,它是子類。出於這個原因,我確實在我的答案中包含了該方法。只有NSPopover纔會根據指定的視圖創建窗口。但是你想要一張模態表。但是,您可以繼承子類並使用彈出窗口來實現模態行爲。 –

+0

好的。謝謝。 :) – Barath