Reputation: 2778
I have simple NSSearchField on view of NSVIewConroller. and connected the NSSearchFIeld's delegate to the view controller. And I have implemented the NSSearcHFieldDelegate Methods as follows:
- (void)searchFieldDidStartSearching:(NSSearchField *)sender NS_AVAILABLE_MAC(10_11);
{
NSLog(@"search field did start: %@", sender.stringValue);
}
- (void)searchFieldDidEndSearching:(NSSearchField *)sender NS_AVAILABLE_MAC(10_11);
{
NSLog(@"search field did end: %@", sender.stringValue);
}
These methods are not getting called on start editing and end editing. But If I implement the NSControl default delegates like controlTextDidChange:
and control:textShouldBeginEditing:
are called on respective events.
Why the NSSearchFieldDelegate methods are not called?..
Upvotes: 3
Views: 1297
Reputation: 102
Step 1: Your controller class needs to inherit from NSSearchFieldDelegate
Step 2: Implement the below 2 methods
func searchFieldDidStartSearching(sender: NSSearchField){
print("searchFieldDidStartSearching \(sender.stringValue)")
}
func searchFieldDidEndSearching(sender: NSSearchField){
print("searchFieldDidEndSearching \(sender.stringValue)")
}
Step 3: in the viewDidLoad, windowDidLoad, awakeFromNib etc set the delegate for the search field as the view controller/ window controller etc
self.searchField.delegate = self
A working sample is as below
// AppDelegate.swift
// NSSearchFieldDelegateSampleCode
// Created by Debasis Das on 27/05/16.
// Copyright © 2016 Knowstack. All rights reserved.
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSSearchFieldDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var searchField:NSSearchField!
func searchFieldDidStartSearching(sender: NSSearchField){
print("searchFieldDidStartSearching \(sender.stringValue)")
}
func searchFieldDidEndSearching(sender: NSSearchField){
print("searchFieldDidEndSearching \(sender.stringValue)")
}
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
self.searchField.delegate = self
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
Upvotes: 2