사용자가 x
버튼을 클릭하여 애플리케이션을 종료 할 때 경고 상자를 표시해야합니다. 어떻게 애플리케이션의 종료 이벤트에 연결하고 경고를 표시 할 수 있습니까? 또는 AppDelegate에에서후크 온 애플리케이션 종료 및 경고 표시
답변
거랑
performSegue(withIdentifier: "segue", sender: nil)
하시기 바랍니다 조언을 통해 뷰 컨트롤러는 .. 당신은 applicationWillTerminate
로 코드를 삽입하고 메시지가 표시되도록 할 수 있어야한다.
편집 : 아마도 segue 대신 모달 경고를 사용하는 것이 좋습니다.
나는 그들이 매우 편리하기 때문에 Segue를 사용하고 싶다.하지만 "applicationWillResignActive
"(백그라운드로 이동)이나 "applicationWillBecomeActive
"과 같은 앱 위임 이벤트에서 스토리 보드로 진행할 수는 없다. "(다시 전경이 됨).
올바른 방법은 경고를 통해 이루어집니다. applicationShouldTerminate
에서이 작업을 수행하고 싶을 수 있습니다. A) 방금 종료하지 않을 충분한 이유가있는 경우 종료 할 수 있거나 B) 사용자가 실제로 종료할지 여부를 선택하도록 할 수 있습니다. 여기
은 빠른 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
}
}
스위프트
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
}
}
감사합니다. 그러나 유스 케이스가 소프트웨어의 평가판을 사용하는 경우 사용자에게 메시지를 전달하는 것이 좋습니다. "평가판을 사용하고 있습니다. 키가 있습니까?" -> "예"; 그런 다음 사용자가 라이센스 데이터를 입력 할 수 있도록하는 시트 유형의 세그먼트를 사용하여 새로운보기 컨트롤러를 실행하십시오 ... 사용자가 응용 프로그램을 종료하지 않기로 결정한 경우 표시 방법이 필요합니다. 또는 경고의 버튼을 누릅니다. 이것이 가능합니까? 제발 조언을 .. – techno
종료 경고에서 "취소"버튼을 누르면 라이센스 키 창 컨트롤러를 표시하는 코드를 편집했습니다. 스토리 보드의 창 컨트롤러에서 올바른 식별자를 설정했는지 확인하십시오. 또한 부모 창이 무엇인지 알면 창을 가져 와서 시트로 표시 할 수도 있습니다. –
업데이트 해 주셔서 감사합니다 ..하지만 다음과 같은 오류가 발생합니다. https://imgur.com/a/CxtZ2를 참조하십시오. Im Xcode 8을 실행 중입니다. – techno
3 당신이 NSApplication''의 대리자 메서드 봤어? 예를 들어,'applicationShouldTerminate :'또는'applicationWillTerminate :'? – mttrb
@mttrb 아니요 ...보세요. – techno
'applicationShouldTerminate'을 시도 했습니까? – Willeke