首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSAlert - MacOS中的多个MacOS

NSAlert - MacOS中的多个MacOS
EN

Stack Overflow用户
提问于 2017-11-09 01:49:49
回答 1查看 2.2K关注 0票数 7

我正在MacOS上使用Swift 4中的一个MacOS设置一个登录对话框。下面是我到目前为止掌握的代码:

代码语言:javascript
复制
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 ("", "")
        }
    }

当我执行unameFieldpassField时,这很好地显示了alert.accessoryView = passField。但我想在同一个对话框中显示这两个字段。正如您所看到的,我已经尝试过NSStackView (和其他),但是我还没有找到显示这两个元素的解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-09 02:30:11

希望会帮助你..。

您可以使用NSStackView和addSubview 2项: unameField和passField。但是您必须为NSStackView设置框架,并在NSStackView上设置2项。

这是我的代码,你可以参考:

代码语言:javascript
复制
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://github.com/nhhthuanck/CustomizeNSAlertMacOS

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47192621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档