简体   繁体   English

从另一个ViewController激活UISearchDisplayController

[英]Activate UISearchDisplayController from another ViewController

I have an app that would benefit from a search function on the main screen. 我有一个可以从主屏幕上的搜索功能中受益的应用程序。 I have written some code that halfway accomplishes this task, but it won't find results and it crashes when tapping (x) on the UISearchBar. 我编写了一些代码,这些代码在一半时就完成了此任务,但找不到结果,并且在UISearchBar上点击(x)时会崩溃。 I have been racking my brain trying to figure out why its not working. 我一直在绞尽脑汁想弄清楚为什么它不起作用。

Main View Controller (with 'search' bar on main screen): 主视图控制器(在主屏幕上带有“搜索”栏):

 ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CenterViewController"];


        [self.navigationController pushViewController:viewController animated:YES];
         [viewController sendSearchRequest:[nameLabel text] sport:[sportLabel text]];

ViewController (view with tableview and UISearchDisplayController) ViewController(带有tableview和UISearchDisplayController的视图)

-(void)sendSearchRequest:(NSString *)request sport:(NSString *)sport{
    [self.filteredArray removeAllObjects]; // First clear the filtered array.
    self.searchText = request;

    /*
     * Search the main list for products whose type matches the scope (if
     * selected) and whose name matches searchText; add items that match to the
     * filtered array.
     */
    [self.filteredArray removeAllObjects]; // First clear the filtered array.
    self.searchText = request;

    /*
     * Search the main list for products whose type matches the scope (if
     * selected) and whose name matches searchText; add items that match to the
     * filtered array.
     */
    NSLog(@"Request: %@", request);
    for (Athlete *athlete in self.athletes) {
        NSComparisonResult result = [[athlete name] compare:request options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [request length])];
        if (result == NSOrderedSame) {
            [self.filteredArray addObject:athlete];
        }
    }
    [self.searchDisplayController setActive: YES animated: YES];
    [self.searchDisplayController.searchBar setText:request];



}

Turns out, it had nothing to do with proper activation. 事实证明,这与正确激活无关。 Basically, I had an array that I was filtering, but it was initialized when the view loaded. 基本上,我有一个要过滤的数组,但是在加载视图时将其初始化。 Well, since I was calling the view very round-a-bout, it never downloaded the array. 好吧,由于我称此视图为一整轮,因此它从未下载阵列。 Instead I downloaded the array from the Main view controller and searched that in the search view controller. 相反,我从主视图控制器下载了数组,并在搜索视图控制器中进行了搜索。 Problem solved. 问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 ViewController 导航到另一个 ViewController - Navigating from ViewController to another ViewController 将现有的 tableView 和 ViewController 与 UISearchDisplayController 一起使用 - Use existing tableView and ViewController with UISearchDisplayController 如何激活具有预设术语的UISearchDisplayController? - how to activate a UISearchDisplayController with pre-set term? 将NSMutableArray从一个viewcontroller复制到另一个viewcontroller? - Copying of NSMutableArray from one viewcontroller to another viewcontroller? 通过另一个ViewController从ViewController获取数据 - Get data from a ViewController via another ViewController 如何从ViewController访问Swift 3中的另一个viewController - How to access from viewController another viewController in Swift 3 IOS 5 - 从另一个ViewController调用ViewController - IOS 5 - Call a ViewController from another ViewController 从另一个 ViewController 呈现故事板 ViewController - Present storyboard ViewController from another ViewController iOS-来自另一个呈现的ViewController的呈现ViewController - iOS - Present viewcontroller from another presented viewcontroller 从主ViewController的按钮中加载另一个ViewController - Load Another ViewController In A Button From Main ViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM