2016-04-22 87 views
3

开卷赛格瑞我有一个的UITableViewController有其向下钻取到另一个的UITableViewController显示大量数据集的单元格。为了提高可用性,我在视图中添加了一个UISearchController。当搜索完成时,用户应该点击单元格,使用展开的segue将用户返回到原始视图控制器,以将其数据返回。还有一个嵌入式导航器,以协助导航。使用在一个UITableView一个UISearchController对细胞选择

如果我没有执行搜索,只需点击它正确展开并填充在原来的小区选择单元格,但如果我做了搜索,然后选择一个小区,我得到了以下错误:

popToViewController:transition:在现有转换或演示文稿发生时调用;导航堆栈将不会更新。

这里是UISearchController我的初始化代码:

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Initialise search controller settings 
    searchController = UISearchController(searchResultsController: nil) 
    searchController.searchResultsUpdater = self 
    searchController.searchBar.sizeToFit() 
    searchController.dimsBackgroundDuringPresentation = false 
    searchController.delegate = self 
    searchController.searchBar.delegate = self 
    tableView.tableHeaderView = searchController.searchBar 
    definesPresentationContext = true 
} 

有没有窍门来实现这一功能呢?

谢谢。

+0

:)即使用户在你的表视图中选择了一个单元格,也会调用相同的方法来执行展开顺序:)这将确保调用展开顺序的一致性:)并且当在UISearchController单元上使用分支时关闭搜索,然后调用performSegueWithIdentifier :)这将确保搜索被首先解散,然后调用unwind segue :)因此,导航堆栈应该没有任何问题:) stil我有疑问留下评论:D –

回答

0

我在以后的iOS 9.0中从searchResultTableView中得到了同样的错误。 我在searchResultTableView中调用unwind segue触发器,但即使我已成功将其数据传递到destinationVC,它也不会移动到destinationVC。

我通过创建自定义展开segue来解决此问题。

让我们来看看应用程序的结构及其逻辑。该结构将是这个样子的:

CustomNavigationController(subclass of UINavigationController) -> MainVC -> push segue -> SearchVC(subclass of UISearchController)

  1. MainVC嵌入CustomNavigationController
  2. 如果从searchResultTableView选择单元格, 的应用程序是使用开卷SEGUE移动到MainVC。我认为你已经在故事板中放松了。因此,您可以将数据设置为提交给destionationVC,并在此方法中调用performSegueWithIdentifier方法。

    // SearchVC or SearchResultVC 
    -(void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    
        CustomData *customData = self.searchResults[[self.tableView indexPathForSelectedRow].row]; 
    
        self.selectedData = customData; 
    
        [self performSegueWithIdentifier:@"PassResult" sender:nil]; 
    
        [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    
    } 
    
  3. 实施prepareForSegue:在SearchVC或 SearchResultVC发送方法将数据传递到destinationVC(MainVC)

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    
        if ([segue.identifier isEqualToString:@"PassResult"]) { 
         MainVC *destVC = (MainViewController *)segue.destinationViewController; 
         destVC.selectedData = self.selectedData; 
        } 
    } 
    
  4. 移动到rootVC在navgiation控制器,当你调用开卷赛格瑞与performSegueWithIdentifier方法。你有两个选择来做到这一点。使用展开segue方法或创建自定义segue。

unwind segue method

如果连接为开卷赛格瑞ID PassResultsearchWithResult方法(在MainVC):

- (IBAction)searchWithResult:(UIStoryboardSegue *)segue 
    { 

     [self.navigationController popToRootViewControllerAnimated:YES]; 

     // Add your code to process your custom data from unwind segue 
     .... 
    } 

custom segue

创建自定义赛格瑞和实施-perform方法。您可以添加自定义动画。我只想在这里转移到RootVC。

@implementation CustomUnwindSegue 

    - (void)perform 
    { 

     UIViewController *sourceViewController = (UIViewController *)self.sourceViewController; 
     UIViewController *destinationViewController = (UIViewController *)self.destinationViewController; 

     [destinationViewController.navigationController popToRootViewControllerAnimated:YES]; 

    } 

    @end 

implementation segueForUnwindingToViewController:fromViewController:CustomNavigationController中的标识符方法。这个方法不能被称为依赖推或模态,但它在这个应用程序结构中起作用。

// refer to : http://hmcreation.net/?p=279 
    -(UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController 
            fromViewController:(UIViewController *)fromViewController 
              identifier:(NSString *)identifier{ 
     UIStoryboardSegue *segue; 

     if ([identifier isEqualToString:@"PassResult"]) { 

      segue = [[CustomUnwindSegue alloc] 
            initWithIdentifier:identifier 
            source:fromViewController 
            destination:toViewController]; 

     }else{ 

      if ([super respondsToSelector:@selector(unwindForSegue:towardsViewController:)]) { 


       NSLog(@"unwinding in iOS9 later"); 

       [super unwindForSegue:segue towardsViewController:toViewController]; 

      } 

      NSLog(@"segueForUnwinding in iOS9 below"); 

      segue = [super segueForUnwindingToViewController:toViewController 
            fromViewController:fromViewController 
              identifier:identifier]; 

     } 

     return segue; 

    } 

在iOS9以后,您可以在自定义navigationController中覆盖unwindForSegue:towardsViewController方法。这个方法的文档描述了它可以处理弹出viewControllers移动到目的地。但它看起来在UISearchController中不起作用。

0

我有同样的问题,并有我的解决方案和解释:当searchController.isActive = true时,它会在navigationController自己推动viewController(其中包含searchController)。所以,试图从viewController展开到initialViewController,给出了你描述的错误。为了避免这个问题,你必须首先关闭searchController。因此,而不是使用此行

performSegue(withIdentifier: "unwindSegueFromViewControllerToInitialViewController", sender: self) 

您必须使用此块:当UISearchController的电池输出:)拨打您的tableview控制器的方法,并从方法进行开卷SEGUE

if searchController.isActive { 

    searchController.dismiss(animated: false, completion: { 
    self.performSegue(withIdentifier: "unwindSegueFromViewControllerToInitialViewController", sender: self) 
    }) 

} else { 

    performSegue(withIdentifier: "unwindSegueFromViewControllerToInitialViewController", sender: self) 

} 
相关问题