我正在MacOS上使用Swift 4中的一个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 ("", "")
}
}当我执行unameField或passField时,这很好地显示了alert.accessoryView = passField。但我想在同一个对话框中显示这两个字段。正如您所看到的,我已经尝试过NSStackView (和其他),但是我还没有找到显示这两个元素的解决方案。
发布于 2017-11-09 02:30:11
希望会帮助你..。
您可以使用NSStackView和addSubview 2项: unameField和passField。但是您必须为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这就是我的结果:
https://stackoverflow.com/questions/47192621
复制相似问题