ios - 无法将 View 推送到 searchResultsController

标签 ios objective-c uinavigationcontroller pushviewcontroller uisearchcontroller

我有一个 TableViewController、一个 SearchResultsController 和一个 WebViewController。当我单击 TableViewController 中的一个单元格时,它会插入 WebViewController 打开一个与该单元格相关的 url。

TableViewController 有一个 UISearchController 将结果过滤到 SearchResultsController 但是当在 SearchResultsController 中单击一个单元格时,WebViewController 不会被推送。

这是 SearchResultsController 的 didSelectRowAtIndexPath 的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WebViewController *webViewController = [[WebViewController alloc] init];

    NSString *extensionURL = (self.searchResults[indexPath.row])[@"url"];
    NSURL *baseURL = [NSURL URLWithString:@"http://www.baseurl.com"];
    NSURL *URL = [NSURL URLWithString:extensionURL relativeToURL:baseURL];

    webViewController.title = (self.searchResults[indexPath.row])[@"title"];
    NSLog(@"%@", URL);
    webViewController.URL = URL;
    [self.navigationController pushViewController:webViewController
                                         animated:YES];

}

我包含了 NSLog 以查看单击单元格时是否发生了任何事情,并且它确实在控制台中显示了 url。这让我觉得问题出在导航 Controller 上。

在网上看似乎 UISearchController 应该包装在 UISearchContainerViewController 中,然后应该放在 navigationController 中。我还是应用程序开发的新手,不知道如何或在哪里做这件事。

我在 TableViewController 的 viewDidLoad 中调用我的 SearchResultsController,如下所示:
- (void)viewDidLoad
{
    [super viewDidLoad];

    SearchResultsViewController *controller = [[SearchResultsViewController alloc] initWithStyle:UITableViewStylePlain];
    [self addObserver:controller forKeyPath:@"results" options:NSKeyValueObservingOptionNew context:nil];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController: controller];
    self.searchController.searchResultsUpdater = self;
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.delegate = self;
    self.definesPresentationContext = YES; 

    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar = self.searchController.searchBar;
    searchBar.searchBarStyle = UISearchBarStyleMinimal;
    UITextField *searchField = [searchBar valueForKey:@"_searchField"];
    searchBar.barTintColor = [UIColor whiteColor];
    searchBar.tintColor = [UIColor whiteColor];

    self.navigationItem.titleView = searchBar;

}

这是我应该嵌入我的 SearchResultsController 的地方吗?

最佳答案

进行搜索时,您将显示 NewModalViewController,因此您只需执行一些步骤即可在显示搜索 Controller 时推送 View Controller

1)self.definesPresentationContext = YES;

在此之后,您应该像这样插入您的 View Controller

2) [self.presentingViewController.navigationController pushViewController:sec 动画:YES];

注意:目前的 modalviewcontroller 没有导航 Controller ,因此正常的推送编码不会工作,因为它没有分配给导航。

享受.....

关于ios - 无法将 View 推送到 searchResultsController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34660256/

相关文章:

ios - UITableView 的 didSelectRowAtIndexPath 方法未被调用

ios - 将默认值或 nil 值设置为函数的泛型类型参数

ios - 不兼容的整数到指针的转换

ios - navController.topViewController 还是 navController.viewControllers.first?

ios - 在 UISplitViewController 中,无法使 showDetailViewController :sender: push onto detail navigationController

ios - 通过获取 Assets 集合排除 iCloud 共享相册

iPhone内存管理: No Need to Clean Up and Release Retained Objects on App Quit?

ios - 来自远程 I/O 单元的原始音频数据的测量单位是什么?

iOS 应用程序试图以模态方式呈现一个事件 Controller

ios - Swift UI 和 Alamofire 图片上传