我需要在用戶通過單擊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