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:
ああ、もちろんそれは、独自のNSRect必要があります!どうもありがとうございます! – tornato7