gpt4 book ai didi

cocoa - 是否有一种标准方法可以让 NSAlert 有条件地关闭它所附加的窗口?

转载 作者:行者123 更新时间:2023-12-03 17:15:56 24 4
gpt4 key购买 nike

我正在编写一个带有首选项面板的超酷应用程序。如果用户打开首选项面板,更改其首选项,然后关闭面板而不保存这些更改,她将收到 NSAlert,通知她可怕的后果。 NSAlert 工作表有两个按钮:“确定”和“取消”。如果用户按“确定”,则工作表和首选项面板应关闭。如果用户按“取消”,则工作表应关闭,但首选项面板不会关闭。

这是相关代码的简化版本:

def windowShouldClose
window_will_close = true

unless self.user_is_aware_of_unsaved_changes
window_will_close = false
alert = make_appropriate_NSAlert # this method returns an NSAlert

alert.beginSheetModalForWindow(self.window,
modalDelegate: self,
didEndSelector: :'userShouldBeAware:returnCode:contextInfo:',
contextInfo: nil)
end

window_will_close
end

def userShouldBeAware(alert, returnCode:returnCode, contextInfo:contextInfo)
if returnCode == NSAlertFirstButtonReturn
self.user_is_aware_of_unsaved_changes = true
end
end

def windowDidEndSheet(notification)
self.window.performClose(self) if self.user_is_aware_of_unsaved_changes
end

我相信我已经让我的超酷应用程序履行了必要的职责,但我担心这不是苹果打算或建议我实现此功能的方式。这感觉就像是黑客攻击,而且没有人明确告诉我这是这样做的方法。在偶然发现这个解决方案之前,我尝试了很多方法。

我想制作模型 mac 应用程序。是否有一些模式或文件对此有更详细的介绍?我已阅读 Apple 的文档 NSAlert类(class)及其关于 Sheet Programming Topics 的文章.

谢谢!

最佳答案

首先,according to the HIG, preference panes should not ask to cancel or confirm.只要用户更改某些内容就自动保存。作为引用,请参阅 iTunes 如何处理其首选项。

如果您确实想要一个保存对话框,这就是我所做的。当用户关闭应用程序窗口并且需要保存内容时,它会派上用场。我用抽屉。这是我存储在 MainMenu .nib 文件中的面板。然后我将其作为实例变量 didWindow 提供,所有这些都使用 InterfaceBuilder 并进行一些单击。在 InterfaceBuilder 中再单击一下,就会注册以下两个方法,以便在适当的事件中调用。

def showSaveDialog
NSApp.beginSheet( doneWindow,
modalForWindow: mainWindow,
modalDelegate: self,
didEndSelector: "didEndSheet:returnCode:contextInfo:".to_sym,
contextInfo: nil)
end

def save(bla)
NSApp.endSheet doneWindow
Pomodoro.new(:text => doneText).save
notifyLabelUpdate
end

def didEndSheet(bla, returnCode: aCode, contextInfo: bla3)
doneWindow.orderOut self
end

请注意 guide on sheets that you point to这是我在 Apple 文档中见过的最令人困惑的指南。

关于cocoa - 是否有一种标准方法可以让 NSAlert 有条件地关闭它所附加的窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6757161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com