【问题标题】:How to dismiss UISearchBar once segued一旦 segued 如何关闭 UISearchBar
【发布时间】:2016-10-18 13:29:49
【问题描述】:

创建一个搜索视图,这是我为主搜索视图所做的代码。

如果我不搜索/过滤,会发生什么情况,uisearchbar 会在 segue 时消失。但是如果我搜索/过滤,那么 uisearchbar 在 seguing 时会停留在导航栏上。

- (void)viewDidLoad {
        [super viewDidLoad];

        // There's no transition in our storyboard to our search results tableview or navigation controller
        // so we'll have to grab it using the instantiateViewControllerWithIdentifier: method
        UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"CompanySearchResultsNavigationController"];

        // Our instance of UISearchController will use searchResults
        self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];

        // The searchcontroller's searchResultsUpdater property will contain our tableView.
        self.searchController.searchResultsUpdater = self;

        self.searchController.hidesNavigationBarDuringPresentation = NO;


        // The searchBar contained in XCode's storyboard is a leftover from UISearchDisplayController.
        // Don't use this. Instead, we'll create the searchBar programatically.
        self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);



        self.navigationItem.titleView = self.searchController.searchBar;


        self.definesPresentationContext = YES;


    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [self.objects count];
    }



    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
        CompanySearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchCell" forIndexPath:indexPath];

        cell.productImageView.file = (PFFile *)object[@"profileImage"];
        cell.productImageView.layer.cornerRadius = cell.productImageView.frame.size.width / 2;
        cell.productImageView.clipsToBounds = YES;
        [cell.productImageView loadInBackground];

        cell.companyNameLabel.text = object[@"username"];

        return cell;
    }

    #pragma mark - UISearchControllerDelegate & UISearchResultsDelegate

    // Called when the search bar becomes first responder
    - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
    {

        // Set searchString equal to what's typed into the searchbar
        NSString *searchString = self.searchController.searchBar.text;


            [self updateFilteredContentForAirlineName:searchString];

        // If searchResultsController
        if (self.searchController.searchResultsController) {

            UINavigationController *navController = (UINavigationController *)self.searchController.searchResultsController;

            // Present SearchResultsTableViewController as the topViewController
            CompanySearchResultsTableViewController *vc = (CompanySearchResultsTableViewController *)navController.topViewController;

            // Update searchResults
            vc.searchResults = self.searchResults;

            // And reload the tableView with the new data
            [vc.tableView reloadData];
        }
    }


    // Update self.searchResults based on searchString, which is the argument in passed to this method
    - (void)updateFilteredContentForAirlineName:(NSString *)companyName
    {

        if (companyName == nil) {

            // If empty the search results are the same as the original data
            self.searchResults = [self.objects mutableCopy];
        } else {

            NSMutableArray *searchResults = [[NSMutableArray alloc] init];

            // Else if the airline's name is
            for (PFObject *company in self.objects) {
                if ([company[@"username"] containsString:companyName]) {

    //                NSString *str = [NSString stringWithFormat:@"%@", company[@"username"]];
    //                [searchResults addObject:str];

                    PFObject *searchedObject = company;
                    [searchResults addObject:searchedObject];

                    NSLog(@"Searched: %@",searchedObject[@"username"]);
                }

                self.searchResults = searchResults;

            }
        }
    }

【问题讨论】:

  • 我假设您想要的是转到应用程序的不同部分,对吗?例如,当用户选择一家公司时,应用程序应该转到显示该公司的某种详细信息的屏幕?
  • 是的,你明白了。
  • 在这种情况下,我看不出我的代码为什么不能工作。
  • 我会给你一个示例项目来说明发生了什么。
  • @moonman239 这是我编辑的示例项目,向您展示我得到了什么。根据我遵循的教程。当您从原始表中继续搜索而不搜索它时。但是当你搜索然后搜索栏停留在顶部。

标签: ios objective-c uitableview uinavigationcontroller uisearchbar


【解决方案1】:

在您的 AirlineTableViewController 中,覆盖 [UIViewController prepareForSegue]:

- (void)prepareForSegue:(UIStoryboardSegue *)segue
                 sender:(id)sender { 
[self.searchController setActive:NO];
}

【讨论】:

  • 所以我有两个 tableViewControllers,初始的 tableview 控制器可以很好地解散。只是当我输入和搜索某些内容时,结果 tableviewcontroller 是一个顶级 viewcontroller,所以我认为这行不通。
猜你喜欢
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
  • 2021-09-30
  • 2020-06-07
  • 1970-01-01
  • 2023-03-31
相关资源
最近更新 更多