我需要在用户通过单击x
按钮退出应用程序时显示警告框。如何挂接应用程序的退出事件并显示警报或视图通过赛格瑞挂机退出应用程序并显示警报
performSegue(withIdentifier: "segue", sender: nil)
请咨询控制器..
我需要在用户通过单击x
按钮退出应用程序时显示警告框。如何挂接应用程序的退出事件并显示警报或视图通过赛格瑞挂机退出应用程序并显示警报
performSegue(withIdentifier: "segue", sender: nil)
请咨询控制器..
在你的appdelegate你应该能够把你的代码放到applicationWillTerminate
,使消息出现在那里。
编辑:你可能会更好使用模态警报,而不是一个segue。
我知道你想用segue来做到这一点,因为它们只是非常方便,但是在应用程序委托事件(如“applicationWillResignActive
”(前往后台)或“applicationWillBecomeActive
” “(再次成为前景)。
正确的做法是通过警报。你可能想在applicationShouldTerminate
中这样做,因为你可能想要A)如果你有一个很好的理由不放弃,或者B)给用户一个选择是否真的退出。
下面是它会怎样看在SWIFT 4:
var licenseWindowController : LicenseWindowController?
func dialogOKCancel(question: String, text: String) -> Bool {
let alert = NSAlert()
alert.messageText = question
alert.informativeText = text
alert.alertStyle = .warning
alert.addButton(withTitle: "OK")
alert.addButton(withTitle: "Cancel")
return alert.runModal() == .alertFirstButtonReturn
}
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
let answer = dialogOKCancel(question: "Ok?", text: "Should we really quit?")
if answer == true
{
return .terminateNow
} else {
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
let answer = dialogOKCancel(question: "Ok?", text: "Should we really quit?")
if answer == true
{
return .terminateNow
} else {
// to bring up a window from your storyboard...
let mainStoryboard = NSStoryboard.init(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil)
self.licenseWindowController = mainStoryboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "LicenseWindowController")) as? LicenseWindowController
if let actualLicenseWC = self.licenseWindowController
{
actualLicenseWC.showWindow(self)
}
return .terminateCancel
}
}
斯威夫特3
var licenseWindowController : LicenseWindowController?
func dialogOKCancel(question: String, text: String) -> Bool {
let alert = NSAlert()
alert.messageText = question
alert.informativeText = text
alert.alertStyle = .warning
alert.addButton(withTitle: "OK")
alert.addButton(withTitle: "Cancel")
return alert.runModal() == NSAlertFirstButtonReturn
}
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplicationTerminateReply {
let answer = dialogOKCancel(question: "Ok?", text: "Should we really quit?")
if answer == true
{
return .terminateNow
} else {
let mainStoryboard = NSStoryboard.init(name: "Main", bundle: nil)
self.licenseWindowController = mainStoryboard.instantiateController(withIdentifier: "LicenseWindowController") as? LicenseWindowController
if let actualLicenseWC = self.licenseWindowController
{
actualLicenseWC.showWindow(self)
}
return .terminateCancel
}
}
Thanks.But使用案例是在用户使用该软件的试用版时向用户发送消息。例如,“您正在使用该试用版,您是否拥有密钥?” - >“是”;然后启动一个新的视图控制器,使用一个表单类型...允许用户输入许可证数据...我需要一种方式来显示segue,如果用户决定不退出应用程序或按下警报中的按钮。这可能吗?请咨询.. – techno
如果在退出警报中点击了“取消”按钮,我编辑了代码以启动许可证密钥窗口控制器。确保在故事板中的窗口控制器上设置了正确的标识符。如果您知道父窗口是什么,您也可以获取该窗口并将其显示为表格。 –
感谢您的更新..但我收到以下错误。请参阅https://imgur.com/a/CxtZ2我正在运行Xcode 8 – techno
你有没有看NSApplication'的'委托方法?例如,'applicationShouldTerminate:'或'applicationWillTerminate:'? – mttrb
@mttrb否...请看看。 – techno
您是否尝试过'applicationShouldTerminate'? – Willeke