【发布时间】:2018-04-21 21:39:35
【问题描述】:
我正在 MacOS 上使用 Swift 4 中的 NSAlert 组合一个登录对话框。这是我到目前为止的代码:
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 (和其他),但我还没有找到显示这两个元素的解决方案。
【问题讨论】: