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

前端之家收集整理的这篇文章主要介绍了ios – 在呈现UINavigationController模式时,segue.destinationViewController为nil前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个UICollectionView,当用户按下一个单元格时,我使用一个故事板以模态方式呈现UINavigationController中的另一个视图控制器.
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"editBookIPad"
                                      sender:indexPath];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue
                 sender:(id)sender
{
    // Did not include code for other segues,but I check which is the current one properly
    UINavigationController *dest = segue.destinationViewController; // This is nil!
    dest.modalPresentationStyle = UIModalPresentationFormSheet;
    DetailsViewController *detailsVC = (id)[dest topViewController];
    detailsVC.stack = self.stack;
    detailsVC.editingMode = 1;
    detailsVC.bookToEdit = [self.fetchedResultsController objectAtIndexPath:sender];
    [self.collectionView deselectItemAtIndexPath:sender
                                        animated:YES];
}

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

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

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

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

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

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

EDIT1

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

EDIT2

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

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard"
                                                     bundle:nil];
UINavigationController *navCon = [storyboard instantiateViewControllerWithIdentifier:@"AllBooksVCDetails"]; // The problematic navigation controller
navCon.modalPresentationStyle = UIModalPresentationFormSheet;
BKSBookDetailsViewController *detailsVC = (id)[navCon topViewController];
detailsVC.stack = self.stack;
detailsVC.editingMode = 1;
detailsVC.bookToEdit = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self presentViewController:navCon
                   animated:YES
                 completion:nil];
[self.collectionView deselectItemAtIndexPath:indexPath
                                    animated:YES];

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

解决方法

将tracecontroller嵌入到NavigationController中时遇到了同样的问题.
override public func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {

        let destinationVC = segue.destinationViewController as! UINavigationController
        let nextViewController = destinationVC.viewControllers[0] as! SecondViewController

        nextViewController.someProperty = someValue
    }

这个链接帮助我:

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

猜你在找的iOS相关文章