在模态表

时间:2017-03-07 22:40:35

标签: swift macos

所以我想通过表格向用户询问模态对话框中的内容:

class MyTextView : NSTextView {
    override func viewWillDraw() {
        dispatch_async(dispatch_get_main_queue()) {
            self.window?.makeFirstResponder(self)
        }
    }
}

private func requestNotification() {
    let alert = NSAlert()
    alert.alertStyle = NSAlertStyle.InformationalAlertStyle
    alert.messageText = "Enter your email address"

    let urlField = MyTextView.init(frame: NSMakeRect(0,0,300,28))
    let urlScroll = NSScrollView.init(frame: NSMakeRect(0,0,300,28))
    urlScroll.hasVerticalScroller = true
    urlScroll.autohidesScrollers = true
    urlField.drawsBackground = true
    urlField.editable = true

    urlScroll.documentView = urlField
    alert.accessoryView = urlScroll

    alert.addButtonWithTitle("Send")
    alert.addButtonWithTitle("Cancel")

    alert.beginSheetModalForWindow(self.window!, completionHandler: { response in
        if response == NSAlertFirstButtonReturn {
            // Send
            let view = (alert.accessoryView as! NSScrollView).documentView as! NSTextView
            let text = view.string! as String
            self.webViewController.loadAlmostURL(text)
        }
    })
}

比使用NSTextField更好地运行。

至少互动从入口开始,但是,我想边框文本视图?

2 个答案:

答案 0 :(得分:1)

代码片段几乎可以做到这一点,除了NSTextField中的border rect之外。

答案 1 :(得分:1)

当尝试使用NSTextField执行此操作时,对viewWillDraw的重复调用似乎会干扰闪烁的光标并在对话框中进行选项卡。要解决这个问题,请在viewWillDraw例程中设置一个标志,只设置firstResponder一次,如下所示:

class AlertTextField : NSTextField {

    var doOnce = true
    override func viewWillDraw() {
        if doOnce {
            DispatchQueue.main.async() {
                self.window?.makeFirstResponder(self)
                self.doOnce = false
            }
        }
    }
}