2017-08-16 81 views
-2

如何防止解僱一個簡單的NSAlert?當應用程序啓動時,我顯示一個包含NSTextField的NSAlert。 用戶應輸入密碼。只有在密碼正確的情況下,才允許用戶使用該應用程序,否則(如果密碼不正確),警報應該留在那裏並再次詢問密碼。如何防止NSAlert被解僱

這是到目前爲止我的代碼(創建警報):

func applicationDidFinishLaunching(_ aNotification: Notification){ 
    let alert = NSAlert() 
    alert.addButton(withTitle: "Send") 
    alert.delegate = self 
    alert.alertStyle = .informational 
    alert.messageText = "Password - Login" 
    alert.informativeText = "Please type in your password: " 

    let txt = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24)) 
    txt.stringValue = "Password:" 


    alert.accessoryView = txt 
    alert.beginSheetModal(for: NSApplication.shared().mainWindow!) { (response) in 
     if (response == NSAlertFirstButtonReturn) { 
      // the alert closes here, is there any way to prevent this? 
     } else { 
      print("No value.") 
     } 
    } 

OS:OS X塞拉利昂, 斯威夫特3

回答

1

可以呈現警報第二次;如果您需要自定義超出該行爲的行爲,則需要避開NSAlert並運行您自己製作的NSWindow或NSPanel。

+0

謝謝,我會嘗試使用NSPanel創建自己的「警報」。順便說一句,你有一個想法,爲什麼我的問題是如此投票?它是否太短,我應該添加細節? –

+1

我不是那種貶低它的人,但問題很短。一般來說,最好是更清楚一點,並提供更多關於你想要做什麼的細節。 –

+0

好的,謝謝。我真的不是故意惹惱別人。我會編輯它並儘量使它更精確。謝謝。 –