【问题标题】:UISearchController does not trigger delegate methods and search bar delegate methodsUISearchController 不触发委托方法和搜索栏委托方法
【发布时间】:2015-04-01 15:33:58
【问题描述】:

所以我有 UISearchController,它显示栏,我可以输入文本但它不会触发委托方法。这是我的代码:

@IBAction func searchTapped(AnyObject) {

    NSLog("search...")

    let cancelButton = UIBarButtonItem(image: UIImage(named: "delete_sign-50"), landscapeImagePhone: nil, style: .Plain, target: self, action: "cancelTapped:")

    var searchController = UISearchController(searchResultsController: nil)
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "enter the text to search"
    searchController.searchResultsUpdater = self;
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.delegate = self
    searchController.searchBar.delegate = self

    self.definesPresentationContext = true;


    UIView.animateWithDuration(0.2, animations: { () -> Void in

        self.navigationItem.rightBarButtonItems = nil
        self.navigationItem.rightBarButtonItems = [cancelButton]
        self.navigationItem.titleView = searchController.searchBar
        searchController.searchBar.sizeToFit()
        self.toolbar.hidden = false
        self.tableView?.hidden = false
    })

    //add first responser to search bar
    searchController.searchBar.becomeFirstResponder()

}

func updateSearchResultsForSearchController(searchController: UISearchController) {

    NSLog("1 %@", searchController.searchBar.text);

}

func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    NSLog("2 %@", text)
    return true
}

文本更改时不会调用这些方法。

不仅如此,我还尝试将 searchController 作为变量添加到我的控制器中,在这种情况下,它工作得非常糟糕——当我点击搜索栏时,它会移出屏幕。

【问题讨论】:

  • 你确认 UISearchController 委托了吗?

标签: ios swift ios8


【解决方案1】:
searchController.active = YES;

这有帮助,不知道为什么在任何地方都没有提到它。

【讨论】:

    【解决方案2】:

    IOS 9

        -(void )viewDidLoad
     _searchController = [[UISearchController alloc] initWithSearchResultsController:self];
        self.navigationItem.titleView  = _searchController.searchBar;
        self.definesPresentationContext = YES;
        _searchController.delegate = self;
        _searchController.searchBar.delegate = self;
        _searchController.searchBar.placeholder = NSLocalizedString(@"SEARCHTITLE", nil);
        [_searchController.searchBar setShowsCancelButton: YES animated: NO];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多