Subclassing a NSSearchField

Hello,


I'd like to add a dropdown to a NSSearchField with different filters, like the search field in iTunes.


I'm new to Cocoa but I was a Flash / Flex develop which has many similarities with the subclassing and I'm looking for the methods to subclass, or is it better to compose with the NSSearchField in a XIB ?


I tried to subclass the draw method with a super call but as soon as I do that, the NSSearchField lose its focus animation.


Thx a lot.

Replies

Hi,

A few mouths later, I just saw your answer... Thx for that !

I'm back on my project which use the ITLibrary (I'm speaking about it here)

Everything works like a charm with my NSSearchField and now I'd like to add a NSMenu to have a more option to filter.

I took a look to your link but I'm not sure to understand how to add the NSMenu...

I have to set a NSMenu to the cell of the NSSearchfield ?

Thx.

Hi,

So basically, I just added a NSMenu to my NSSearchField :

let theMenu = NSMenu(title: "Filters")
        let theMenus = ["all", "artist", "title", "album"]
        for theName in theMenus {
            let theMenuItem = NSMenuItem(title: theName, action: nil, keyEquivalent: "")
            theMenu.addItem(theMenuItem)
            theMenuItem.isEnabled = true
        }        
        searchField.searchMenuTemplate = theMenu

Problem : the items are disabled and I don't know how to enable it :

What's wrong ?

Thx.

Problem : the items are disabled and I don't know how to enable it :

What's wrong ?

To make menu items enabled, there needs to be a responder which can respond to the action.

class ViewController: NSViewController {
    
    @IBOutlet weak var searchField: NSSearchField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        let theMenu = NSMenu(title: "Filters")
        let theMenus = ["all", "artist", "title", "album"]
        for theName in theMenus {
            //↓ Specify `action:`.
            let theMenuItem = NSMenuItem(title: theName, action: #selector(menuItemSelected), keyEquivalent: "")
            theMenu.addItem(theMenuItem)
            theMenuItem.isEnabled = true
        }
        searchField.searchMenuTemplate = theMenu
    }

    //↓ Define the `action` method used in `NSMenuItem(title:action:keyEquivalent:)`. 
    @objc func menuItemSelected(_ sender: Any) {
        print(sender, type(of: sender))
    }
    
    //...    
    
}

Generally, you should better show more context (especially your code), to get better responses sooner.

Thx for your answer, and yes, I had to add a selector... Not very intuitive but it is like that.

So Now I can manage my item selection and do my filter but I'm looking for how to achieve that.

I wrote a message here but no answer yet.

Can U help me ?

Thx.

  • you should better show more context (especially your code), to get better responses sooner Why don't you show your code enough to reproduce your issue? A few screen shots and very little explanation would not be appealing to many readers.

Add a Comment