Description
I am trying to use NSSegmentedControls
to transition between Child ViewControllers. The ParentViewController
is located in Main.storyboard
and the ChildViewControllers
are located in Assistant.storyboard
. Each ChildViewController has a SegmentedControl divided into 2 Segments and their primary use is to navigate between the ChildViewControllers. So they are set up as momentaryPushIn
rather than selectOne. Each ChildViewController uses a Delegate to communicate with the ParentViewController.
So in the ParentViewController I added the ChildViewControllers as following:
/// 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]
}
So far so good. The ExampleChildViewController
has an NSTextField
instance. While I am in the scope of the TextField, I can trigger the action of the SegmentedControls. Its navigating forward and backward as it should. But once I leave the scope of the TextField I can still click the Segments, but they are not triggering any action. They should be able to navigate forward and backward even if the TextField is not the current "First Responder" of the application. I think I am missing something out here, I hope anyone can help me with this. I know the problem is not the NSSegmentedControl
because I am seeing the same behavior with an NSButton
, which is configured as Switch/Checkbox, in the SiblingViewController. I just don't have any idea anymore what I am doing wrong.
It`s my first time asking a question myself here, so I hope the way I am doing is fine for making progress with the solution. Let me know if I can do something better/different or if I need to provide more information about something.
Thanks in advance!
Additional Information
For the sake of completeness:
The
ParentViewController
itself is embedded in aContainerView
, which is owned by theRootViewController
. I can't imagine this does matter in any way, but this way we are not missing something out.
I am actually not showing the navigation action, because I want to keep it as simple as possible. Furthermore the action is not problem, it does what I want it to do. Correct me if I am wrong with this.
Possible solutions I found while researching, which did not work for me:
- Setting window.delegate of the ChildViewControllers to NSApp.windows.first?.delegate
- Setting the ChildViewController to
becomeFirstResponder
in itsfunc viewWillAppear()
visualEffectView.addSubview(self.children[0].view, positioned: NSWindow.OrderingMode.above, relativeTo: nil
)
Related problems/topics I found while researching:
- 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