简体   繁体   English

使用Swift将NSView添加到响应链的指南

[英]Guide for adding NSView to responder chain with Swift

I've read this document. 我已经阅读了这份文件。 Seen this SO question (amongst others ). 看到了这个 SO问题(以及其他问题)。 And had a look at this tutorial. 并看了一下教程。 But for the life of me I cannot get my custom storyboard segue to work. 但是对于我的一生,我无法让自定义情节提要板正常工作。 I'm using the code found in this SO answer to bring on new views and their controllers. 我正在使用 SO答案中找到的代码来引入新视图及其控制器。

Basically, the user is presented with 5 buttons in the initial view and clicking any one will cause another view to slide in from the left. 基本上,在初始视图中为用户显示5个按钮,单击任何一个按钮都将导致另一个视图从左侧滑入。 All works fine, any NSButtons that are placed on the new views receive clicks and do what they're supposed to. 一切正常,放置在新视图上的所有NSButtons都会获得点击并执行应有的功能。 However, text fields are completely unaccessible: you can select any text within but not overwrite. 但是,文本字段完全不可访问:您可以选择其中的任何文本,但不能覆盖。

I'm assuming that I haven't injected the new view/view controler into the responder chain correctly (but then why do the buttons work and the text fields not?). 我假设我没有将新的视图/视图控制器正确地注入响应者链中(但是为什么按钮可以工作而文本字段却不能?)。

Judicious use of 明智地使用

self.resignFirstResponder()

and

newViewController.becomeFirstResponder()

always return true. 始终返回true。 I've placed them in the various initial lifecycle functions of the new views as well as in the actual custom NSViewControllerPresentationAnimator at the end of the animation. 我将它们放置在新视图的各种初始生命周期函数中,以及动画结束时的实际自定义NSViewControllerPresentationAnimator中。

If the segue is a preset sheet, modal, popover or whatever then the new view receives all actions/typing just fine. 如果segue是预设的工作表,模式,弹出框或其他内容,则新视图会很好地接收所有操作/键入。 So it's definitely my code. 所以这绝对是我的代码。

I know it's not good form but I'm going mad: can someone please provide a step by step example using Swift? 我知道这不是很好的形式,但是我很生气:有人可以使用Swift一步一步地举例吗?

[EDIT] Arghhhh! [编辑] 啊! It seems to be that because I had the 'Title Bar' unchecked on my app window then it refused to take first responder. 似乎是因为我的应用程序窗口中的“标题栏”未被选中,所以它拒绝接受第一响应者。 Shame, I quite liked the clean window with no title look.... 真可惜,我很喜欢没有标题的干净窗户。

In my case I wanted there to be no Title Bar to the App window. 就我而言,我希望App窗口没有标题栏。 When the Title Bar option is unchecked in IB Attributes section of the storyboard window then you need to sub-class NSWindow and override canBecomeMain and canBecomeKey : 如果在情节canBecomeMain窗口的“ IB属性”部分中未选中“标题栏”选项,则需要子类化NSWindow并覆盖canBecomeMaincanBecomeKey

import Cocoa

class BTVWindow: NSWindow {

    override var canBecomeKey: Bool{
        get {
            return true
        }
    }

    override var canBecomeMain: Bool{
        get {
            return true
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM