2017-11-09 5 views
0

MacOSのSwift 4のNSAlertを使用してログインダイアログを作成しています。ここで私が持っているコードは、これまでです:NSAlertの複数のNSTextField - MacOS

func dialogOKCancel(question: String, text: String) -> (String, String) { 
     let alert = NSAlert() 
     alert.messageText = question 
     alert.informativeText = text 
     alert.alertStyle = .warning 

     alert.addButton(withTitle: "Login") 
     alert.addButton(withTitle: "Cancel") 


     let unameField = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24)) 
     let passField = NSSecureTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24)) 

     let stackViewer = NSStackView() 
     stackViewer.addSubview(unameField) 

     alert.accessoryView = stackViewer 

     let response: NSApplication.ModalResponse = alert.runModal() 

     if (response == NSApplication.ModalResponse.alertFirstButtonReturn) { 
      return (unameField.stringValue, passField.stringValue) 
     } else { 
      return ("", "") 
     } 
    } 

これは私がalert.accessoryView = passFieldを行う際unameFieldまたはpassFieldのいずれかを表示するだけで正常に動作します。しかし、私は両方のフィールドを同じダイアログに表示したい。私がNSStackView(と他のもの)を試してみましたが、私は両方の要素を示すための解決策を見出していません。 unameFieldとpassField:

答えて

2

希望はあなたがNSStackViewとaddSubview 2アイテムを使用することができます...

をお手伝いします。 しかし、NSStackViewのフレームとNSStackViewの2つの項目を設定する必要があります。

let unameField = NSTextField(frame: NSRect(x: 0, y: 2, width: 200, height: 24)) 

let passField = NSSecureTextField(frame: NSRect(x: 0, y: 28, width: 200, height: 24)) 

let stackViewer = NSStackView(frame: NSRect(x: 0, y: 0, width: 200, height: 58)) 

stackViewer.addSubview(unameField) 

stackViewer.addSubview(passField) 

alert.accessoryView = stackViewer 

をそして、これは私の結果である:

これは私のコードで、あなたが参照できる

my result

+0

ああ、もちろんそれは、独自のNSRect必要があります!どうもありがとうございます! – tornato7

関連する問題