【发布时间】:2020-06-07 18:40:50
【问题描述】:
每次用户按下回车键时,都会创建一个新的 NSTextView。这工作正常,新的 NSTextView 成为第一响应者,这也是我的目标。但是,尽管它是第一响应者,但光标并没有移动到新的 NSTextView 中。
下面是我的代码:
func makeNSView(context: NSViewRepresentableContext<TextView>) -> TextView.NSViewType {
let textView = NSTextView()
textView.textContainer?.lineFragmentPadding = 10
textView.textContainerInset = .zero
textView.font = NSFont.systemFont(ofSize: 12)
textView.becomeFirstResponder()
//NSApp.activate(ignoringOtherApps: true) ----> doesn't make difference but is supposed to help switch cursor
print("\(textView) is first responder") //proof that the first responder is shifting, but the cursor does not move with it for some reason
return textView
}
我已尝试按照其他答案的建议使用此行:
NSApp.activate(ignoringOtherApps: true)
但是,它没有任何区别。我也尝试在选定范围内插入文本,但它不会在显示的任何 NSTextView 中插入文本,无论它们是否是第一响应者。
有哪些方法可以将光标移动到不同的 NSTextView(最好是第一响应者)?
如果需要更多信息,请告诉我。
【问题讨论】:
-
当您要求它成为第一响应者时,您的视图不是视图层次结构的一部分。将文本视图添加到窗口后成为第一响应者
标签: swift macos swiftui first-responder text-cursor