Finally got back to this last night. Problem solved. I had the wrong object in the toolbar for the search item. For this to work I needed a search field in the toolbar instead of a search field tool bar item.
Post
Replies
Boosts
Views
Activity
Thanks Claude for your answer, the search field is last but I changed the the code to the following: This doesn't not crash but it does not add the menu in the toolbar search item. Also, as I mentioned I have basically the same code that works fine in another customer view controller. So, I don't see why this new code seems to behave differently.
What I am wanting to do is the following:
Add a search field item to the window controller toolbar
Expose the search field from the toolbar to the view controller with the viewWillAppear override
From the view controller code add my search options (In this case ALL, Number, Label and Amount) as well as the search function itself.
var itemSearchField: NSSearchField = NSSearchField()	 // Global variable
// ToolBar Search
override func viewWillAppear() {
guard let toolbar = self.view.window?.toolbar else {
return
}
for item in toolbar.items where item.view is NSSearchField {
itemSearchField = (item.view as? NSSearchField)!	 // Point itemSearchField to toolbar search item
}
createMenuForSearchField()	 // Create Menu for Search Field
itemSearchField.target = self
itemSearchField.action = #selector (itemSearch (sender:))
}
Also, I tried a few variations of you code but no success.
Please advise
Never mind I see how it works. I have to be the change color function in my custom split view controller.
Thanks
Thanks for the reply.
I have tried putting the IBAction to change the color in both of the children view controller with the same results. I was trying to change the right side of the split view. Based on your reply I created a sub class for the Parent Controller and used my code above in it. But I was not able to connect the IBAction to a button in the Parent Controller. So, I don't think I quite understand your reply?
I wish I could simply upload my test project and you could take a look at it to see what I am talking about. However, in lieu of that, If you have time or anyone else you could create my test project by the following.
Using Xcode 12 create a new macOS project called ColorSetting
Add a Window Controller with a side bar.
In either of the child views or both create a custom NSViewController
Copy and paste the code above in the custom Class
add a button in a view and connect it to the IBAction in the custom class
At any rate I will continue to investigate this issue.
With the help from another developer I got this all working. Here is the solution:
This assumes the search field is last item.
override func viewWillAppear() {
				guard let toolbar = self.view.window?.toolbar else {
						return
				}
								
				guard let searchFieldToolbarItem = toolbar.items.last else {
						return
				}
				guard let searchField = searchFieldToolbarItem.view as? NSSearchField else {
						return
				}					
				searchField.target = self
				searchField.action = #selector (procSearchFieldInput (sender:))
		}
		@objc func procSearchFieldInput (sender:NSSearchField) {
				print ("\(#function): \(sender.stringValue)")
		}
I currently have a view controller which has a search field in it connected by an @IBOutlet and I have this working fine. So, I wanted to move the search field from the view to the toolbar of the window controller. I initially though I could connect the new search field to the view controller to enable the search function to search a table view in my custom view controller class. However, I ran in an issue exposing the search field in the toolbar to my custom view controller. Does this help? I will look over your response tomorrow and see what I can glean from it.
thanks for the reply
I have made some progress on this by using a custom window like in the following code:
import Cocoa
class CustWindow: NSWindow {
@IBOutlet var toolBar: NSToolbar!
}
connecting my toolbar search filed to the outlet above then in the view controller I have the following:
override func viewWillAppear() {
if let window = self.view.window as? CustWindow {
print ("\(window.toolbar.identifier)")
let toolBarItem = NSToolbar(identifier: window.toolbar.identifier)
	 print(toolBarItem)
}
}
Sounds like your way may be easier. What did you mean by "Disclosing the toolbar"?
I CTLR dragged to the first responder and connected it to a @IBAction func but the action func never gets called when I select the color item in the toolbar? Do you or any one else have an example of how to do this or would you give me some more detail on the NSColorChanging protocol?
I have read the apple documentation on this and I am still not clear on how to do this?
Thanks I used the tag value to test for which field. Thanks
It seems there are several ways to communicate from one VC to another. So, I looked at a couple. One using Key Value Observation and another using a notification in @objc. So, I used the @objc route as it was more straight forward for what I needed. It goes something like the following
Register
let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(userLoggedIn), name: Notification.Name("UserLoggedIn"), object: nil)
Post
nc.post(name: Notification.Name("UserLoggedIn"), object: nil)
Then an @objc func userLoggedIn to handle the action of the post.
At any rate I have this working.
Thanks
Yes I want the two VCs to communicate with each other. I have been looking for examples of this as well as more information on how this is coded. I saw one old video on this subject but it seems to have changed as to the way this can be done.
So, I have started with the following code:
NSUserNotificationCenter.default.addObserver(observer: NSObject, forKeyPath: String, options: NSKeyValueObservingOptions>, context: UnsafeMutableRawPointer)
but not sure if this is what you are talking about as well as what to code for the arguments. Would you mind a little more detail how this is done?
Thanks
Yes, that took care of it. However I did have to add width constraints to the other text fields as well to get the desired with. With just one constraint set it used this value for the other text fields.
Another unexpected occurrence .. I added a popup button to the stack view with the text field and then they all show up, but the width of the fields is locked as well as truncated. I trying something that just does not work the way I am thinking?
Success. Thanks Claude for all the help. Although you gave me the answer I did learn from it.
I have the following: A window with a custom view controller - CustViewController: NSViewController
Inside this I have the right mouse detect.