未调用Cocoa-NSearchFieldDelegate方法



在NSVIewConroller的视图中,我有一个简单的NSSearchField。并将NSSearchFIeld的委托连接到视图控制器。我已经实现了NSSearchHFieldDelegate方法如下:

- (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);
}

在开始编辑和结束编辑时不会调用这些方法。但是,如果我实现NSControl,则会在相应的事件上调用controlTextDidChange:control:textShouldBeginEditing:等默认委托。

为什么不调用NSSearchFieldDelegate方法?。。

步骤1:您的控制器类需要从NSSearchFieldDelegate 继承

步骤2:实现以下2种方法

    func searchFieldDidStartSearching(sender: NSSearchField){
        print("searchFieldDidStartSearching (sender.stringValue)")
    }
    func searchFieldDidEndSearching(sender: NSSearchField){
        print("searchFieldDidEndSearching (sender.stringValue)")
    }

步骤3:在viewDidLoad、windowDidLoad和wakeFromNib等中,将搜索字段的委托设置为视图控制器/窗口控制器等

 self.searchField.delegate = self

工作样品如下

//  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
    }

}

相关内容

  • 没有找到相关文章

最新更新