无法将视图推送到searchResultsController

问题描述 投票:0回答:3

我有一个 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。这让我认为问题出在 navigationController 上。

在网上查看似乎 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 的地方吗?

ios objective-c uinavigationcontroller pushviewcontroller uisearchcontroller
3个回答
18
投票

在进行搜索时,您将呈现 NewModalViewController,因此您需要做的很简单,就是在呈现搜索控制器时推送视图控制器的一些步骤

1)self.definesPresentationContext = YES;

在此之后,你应该像

那样推送你的视图控制器

2) [self.presentingViewController.navigationController PushViewController:秒动画:是];

注意:当前的 modalviewcontroller 没有导航控制器,因此正常的推送编码将无法工作,因为它没有分配给导航。

享受......


0
投票

您可以在 SearchResultsController 中创建一个属性:

@property (nonatomic, strong) UINavigationController *navController;

然后在 TableViewController 的 updateSearchResultsForSearchController 中设置 navController = self.navigationController。

然后你可以在 SearchResultsController 的 didSelectRowForIndexPath 中执行此操作:

[self.navController pushViewController:webViewController
                                     animated:YES];

在未能使阿伦的上述解决方案发挥作用后,这种方法对我有用。


0
投票

在显示 searchResultsController 的视图控制器中使用:

definesPresentationContext = true

接下来在 searchResultsController 中你按下另一个控制器,如下所示:

presentingViewController?.navigationController?.pushViewController(anotherVC, animated: false)

© www.soinside.com 2019 - 2024. All rights reserved.