繁体   English   中英

从另一个ViewController激活UISearchDisplayController

[英]Activate UISearchDisplayController from another ViewController

我有一个可以从主屏幕上的搜索功能中受益的应用程序。 我编写了一些代码,这些代码在一半时就完成了此任务,但找不到结果,并且在UISearchBar上点击(x)时会崩溃。 我一直在绞尽脑汁想弄清楚为什么它不起作用。

主视图控制器(在主屏幕上带有“搜索”栏):

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


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

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];



}

事实证明,这与正确激活无关。 基本上,我有一个要过滤的数组,但是在加载视图时将其初始化。 好吧,由于我称此视图为一整轮,因此它从未下载阵列。 相反,我从主视图控制器下载了数组,并在搜索视图控制器中进行了搜索。 问题解决了。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM