ios – 在呈现UINavigationController模式时,segue.destinationViewController为nil

前端之家收集整理的这篇文章主要介绍了ios – 在呈现UINavigationController模式时,segue.destinationViewController为nil前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个UICollectionView,当用户按下一个单元格时,我使用一个故事板以模态方式呈现UINavigationController中的另一个视图控制器.
  1. - (void)collectionView:(UICollectionView *)collectionView
  2. didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. [self performSegueWithIdentifier:@"editBookIPad"
  5. sender:indexPath];
  6. }
  7.  
  8.  
  9. - (void)prepareForSegue:(UIStoryboardSegue *)segue
  10. sender:(id)sender
  11. {
  12. // Did not include code for other segues,but I check which is the current one properly
  13. UINavigationController *dest = segue.destinationViewController; // This is nil!
  14. dest.modalPresentationStyle = UIModalPresentationFormSheet;
  15. DetailsViewController *detailsVC = (id)[dest topViewController];
  16. detailsVC.stack = self.stack;
  17. detailsVC.editingMode = 1;
  18. detailsVC.bookToEdit = [self.fetchedResultsController objectAtIndexPath:sender];
  19. [self.collectionView deselectItemAtIndexPath:sender
  20. animated:YES];
  21. }

现在,我的问题是segue.desinationViewController返回nil(如代码片段中的注释所示).

只是为了调试,我将UINavigationController更改为另一个视图控制器,我没有问题.我不知道是否从modal更改为push作为转换样式会有所帮助,因为它不可能推送UINavigationController(发生崩溃的情况就是这样).

我清理了项目和构建文件夹,然后重新启动了我的计算机(以及Xcode).

这是运行应用程序时的样子:

在寻找类似的问题时,我发现了这一点.大多数其他问题是关于在目标视图控制器上设置的属性为nil(例如this).

我使用Xcode 5.1.1并将iOS 7.0作为开发目标.

EDIT1

现在我的应用程序的所有部分都出现了同样的问题(UINavigationController以模态方式呈现).但是,它只在某些情况下发生,但每次segue.destinationViewController仍为零.

EDIT2

我用这个替换了prepareForSegue代码(手动完成):

  1. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard"
  2. bundle:nil];
  3. UINavigationController *navCon = [storyboard instantiateViewControllerWithIdentifier:@"AllBooksVCDetails"]; // The problematic navigation controller
  4. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  5. BKSBookDetailsViewController *detailsVC = (id)[navCon topViewController];
  6. detailsVC.stack = self.stack;
  7. detailsVC.editingMode = 1;
  8. detailsVC.bookToEdit = [self.fetchedResultsController objectAtIndexPath:indexPath];
  9. [self presentViewController:navCon
  10. animated:YES
  11. completion:nil];
  12. [self.collectionView deselectItemAtIndexPath:indexPath
  13. animated:YES];

这很有效.所以我认为问题出在故事板上.

解决方法

将tracecontroller嵌入到NavigationController中时遇到了同样的问题.
  1. override public func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
  2.  
  3. let destinationVC = segue.destinationViewController as! UINavigationController
  4. let nextViewController = destinationVC.viewControllers[0] as! SecondViewController
  5.  
  6. nextViewController.someProperty = someValue
  7. }

这个链接帮助我:

https://www.andrewcbancroft.com/2015/06/02/access-sub-controllers-from-a-uinavigationcontroller-in-swift/

猜你在找的iOS相关文章