【发布时间】:2019-01-17 15:00:38
【问题描述】:
说明
我正在尝试使用NSSegmentedControls 在子视图控制器之间进行转换。 ParentViewController 位于Main.storyboard,ChildViewControllers 位于Assistant.storyboard。每个 ChildViewController 都有一个 SegmentedControl,分为 2 个 Segment,它们的主要用途是在 ChildViewController 之间导航。所以它们被设置为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 的范围,我仍然可以单击 Segments,但它们不会触发任何操作。即使 TextField 不是应用程序的当前“第一响应者”,它们也应该能够向前和向后导航。我想我在这里遗漏了一些东西,我希望任何人都可以帮助我。我知道问题不在于NSSegmentedControl,因为我在SiblingViewController 中看到NSButton 的行为相同,该NSButton 配置为开关/复选框。我只是不知道我做错了什么。
这是我第一次在这里自己提出问题,所以我希望我的做法能够很好地解决问题。让我知道我是否可以做得更好/不同,或者我是否需要提供有关某事的更多信息。
提前致谢!
其他信息
为了完整起见:
ParentViewController本身嵌入在ContainerView中, 它归RootViewController所有。我无法想象这会 无论如何都很重要,但这样我们就不会错过任何东西。
我实际上并没有显示导航动作,因为我想 尽可能简单。而且动作没问题, 它做我想做的事。如果我错了,请纠正我。
我在研究时发现的可能解决方案,这对我不起作用:
- Setting window.delegate of the ChildViewControllers to NSApp.windows.first?.delegate
- Setting the ChildViewController to
becomeFirstResponderin itsfunc viewWillAppear() visualEffectView.addSubview(self.children[0].view, positioned: NSWindow.OrderingMode.above, relativeTo: nil)
我在研究时发现的相关问题/主题:
- Basic segmented control not working
- Adding and Removing Child View Controllers
- NSSegmentedControl - Odd appearance when placed in blur view
- How to set first responder to NSTextView in Swift?
- How to use #selector in Swift 2.2 for the first responder
- Accessing methods, actions and/or outlets from other controllers with swift
- How to use Child View Controllers in Swift 4.0 programmatically
- Container View Controllers
- issues with container view
- Control a NSTabViewController from parent View
- How to detect when NSTextField has the focus or is it`s content selected cocoa
【问题讨论】:
标签: swift macos cocoa action nssegmentedcontrol