Swift macOS SegmentedControl操作未调用

726F6F74

描述

我试图用来NSSegmentedControls在子ViewController之间过渡。ParentViewController位于Main.storyboard和的ChildViewControllers位于Assistant.storyboard每个ChildViewController都有一个细分控件,该细分控件分为2个细分,它们的主要用途是在ChildViewControllers之间导航。因此,它们被设置为momentaryPushIn而不是selectOne。每个ChildViewController使用一个Delegate与ParentViewController通信。

因此,在ParentViewController中,我添加了ChildViewControllers,如下所示:

/// The View of the ParentViewController configured as NSVisualEffectView
@IBOutlet var visualEffectView: NSVisualEffectView!

var assistantChilds: [NSViewController] {
    get { return [NSViewController]() }
    set(newValue) {
        for child in newValue { self.addChild(child) }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.
    addAssistantViewControllersToChildrenArray()
}

override func viewWillAppear() {
    visualEffectView.addSubview(self.children[0].view)
    self.children[0].view.frame = self.view.bounds
}

private func addAssistantViewControllersToChildrenArray() -> Void {
    let storyboard = NSStoryboard.init(name: "Assistant", bundle: nil)
    let exampleChild = storyboard.instantiateController(withIdentifier: "ExampleChild") as! ExampleChildViewController
    let exampleSibling = storyboard.instantiateController(withIdentifier: "ExampleSibling") as! ExampleSiblingViewController

    exampleChild.navigationDelegate = self
    exampleSibling.navigationDelegate = self

    assistantChilds = [exampleChild, exampleSibling]
}

到现在为止还挺好。ExampleChildViewController有一个NSTextField实例。在TextField范围内时,我可以触发SegmentedControls的操作。它应该向前和向后导航。但是,一旦离开TextField的范围,我仍然可以单击细分,但它们不会触发任何操作。即使TextField不是应用程序的当前“第一响应者”,他们也应该能够前后导航。我想我在这里遗漏了一些东西,希望任何人都可以帮助我。我知道问题不在于,NSSegmentedControl因为我NSButton在SiblingViewController中看到与配置为Switch / Checkbox的相同的行为我只是不知道我在做什么错。

这是我第一次在这里自己问问题,所以我希望我的工作方式对解决方案有所帮助。让我知道我是否可以做得更好/不同,或者是否需要提供有关某事的更多信息。

提前致谢!


附加信息

为了完整性:

ParentViewController自身嵌入到ContainerView,这是由拥有RootViewController我无法想象这确实有关系,但是这样我们就不会错过任何东西。


实际上,我没有显示导航操作,因为我想使其尽可能简单。此外,该操作不是问题,它可以执行我想要的操作。如果我错了,请纠正我。


我在研究时发现了可能的解决方案,但对我不起作用:


我在研究时发现的相关问题/主题:

726F6F74

let parentViewControllerInstance = self.parent as! ParentViewController
segmentedControl.target = parentViewControllerInstance

就我而言,我只需要将委托设置为sendAction方法的目标即可


背景

好的,阅读了数小时的AppKit文档后,我现在可以回答我自己的问题了。

首先,调试用户界面表明问题绝对不在ViewHierarchy中。调试界面

因此,我尝试考虑NSButton和NSSegmentedControl的性质。在某些时候,我注意到这两个都是NSControl的子类。

class NSSegmentedControl : NSControl
class NSButton : NSControl

AppKit文档说:

讨论区

按钮是用于在您的应用内启动操作的标准控件。您可以配置具有许多不同视觉样式的按钮,但是行为是相同的。单击后,按钮将调用其关联的目标对象的操作方法(...)您可以使用操作方法来执行特定于应用程序的任务。

The bold text points to the key of the solution – of its associated target object. Typically I define the action of an control item like this:

button.action = #selector(someFunc(_:))

This causes the NSControl instance to call this:

func sendAction(_ action: Selector?, to target: Any?) -> Bool

Parameter Description from the documentation:

Parameters

theAction

The selector to invoke on the target. If the selector is NULL, no message is sent.

theTarget

The target object to receive the message. If the object is nil, the application searches the responder chain for an object capable of handling the message. For more information on dispatching actions, see the class description for NSActionCell.

总之,正在触发操作方法的NSControl实例(在我的情况下为NSSegmentedControl)没有将其操作发送到的目标。因此,它只能在响应者链中发送其操作方法-当第一个响应者位于另一个视图中时,该方法显然为零。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章