Applescript objc 如何在单独的.XIB文件中使用工作表窗口显示NSAlert?
我有一个按钮来执行一项任务,如果在此过程中发生错误,将使用NSAlert Sheet窗口显示警告 现在我在一个项目中使用多个.XIB文件,我无法在这些文件中显示工作表窗口。XIB有什么方法可以做到这一点吗? 提前谢谢Applescript objc 如何在单独的.XIB文件中使用工作表窗口显示NSAlert?,applescript-objc,Applescript Objc,我有一个按钮来执行一项任务,如果在此过程中发生错误,将使用NSAlert Sheet窗口显示警告 现在我在一个项目中使用多个.XIB文件,我无法在这些文件中显示工作表窗口。XIB有什么方法可以做到这一点吗? 提前谢谢 on clickedButton:sender try --some code here... on error errorMsg set alert to current application's NSAlert's alloc's init()
on clickedButton:sender
try
--some code here...
on error errorMsg
set alert to current application's NSAlert's alloc's init()
tell alert
its setMessageText:"This is an ERROR"
its setInformativeText: "Error: " & errorMsg
its setAlertStyle:2
its beginSheetModalForWindow:theWindow modalDelegate:me didEndSelector:(missing value) contextInfo:(missing value)
end tell
end try
end clickedButton:
如果没有用于放置工作表的窗口,则需要运行“正常警报”对话框。您可以使用NSApp的主窗口测试窗口,如果没有主窗口,则主窗口将丢失值,并根据结果执行警报的运行模式或工作表:
use framework "Cocoa"
use scripting additions
property theWindow : missing value -- this will be the main window...
property sheetOrNot : true -- ... or not, depending on this flag
on run -- this lets the example run from the Script Editor if you forget the main thread thing
my performSelectorOnMainThread:"doWindow" withObject:(missing value) waitUntilDone:true
my performSelectorOnMainThread:"doAlert" withObject:(missing value) waitUntilDone:true
end run
on doWindow() -- create a window to play with
set theWindow to current application's NSWindow's alloc's initWithContentRect:{{200, 400}, {400, 200}} styleMask:7 backing:(current application's NSBackingStoreBuffered) defer:true
set theWindow's releasedWhenClosed to true
if sheetOrNot then tell theWindow to makeKeyAndOrderFront:me
end doWindow
on doAlert() -- show the alert
try
--some code here...
error "oops" -- ...that went badly
on error errorMsg
set alert to current application's NSAlert's alloc's init()
tell alert
its setMessageText:"This is an ERROR"
its setInformativeText:("Error: " & errorMsg)
its setAlertStyle:2
if current application's NSApp's mainWindow() is not missing value then
its beginSheetModalForWindow:theWindow modalDelegate:me didEndSelector:(missing value) contextInfo:(missing value)
else -- no window, so do a normal modal dialog
its runModal()
end if
end tell
end try
end doAlert
如果您的意思是,即使您没有可放置工作表的窗口,也要显示警报,只需执行beginSheet。。。或者基于应用程序有一个主窗口的运行模式。@red\u威胁在这种情况下窗口是一个主窗口。如果你没有主窗口,NSApp的主窗口将丢失值,你将没有一个窗口来放置工作表,所以你可以用它来做alert的运行模式或工作表。@red\u威胁,我为你的愚蠢感到抱歉,但是你能给我一个上面代码的例子吗?我想我有精神障碍我明白了,谢谢。我以为可以在自定义视图中显示图纸窗口。显然不是。床单用在窗户上;如果你不一定需要一个警报对话框,可以看看?