当呈现UINavigationController模态时,segue.destinationViewController为nil

use*_*212 7 xcode ios uistoryboardsegue

我有一个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];
}
Run Code Online (Sandbox Code Playgroud)

故事板

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

只是为了调试,我改为UINavigationController另一个视图控制器,我没有问题.我不知道是否从modal更改为push作为过渡样式会有所帮助,因为它不可能推动a UINavigationController(发生崩溃事件就是这样).

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

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

问题截图

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

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

EDIT1

现在我的应用程序的所有部分都出现了同样的问题(UINavigationController模拟地呈现了所有部分).但是,它只在某些情况下发生,但每次segue.destinationViewController都是如此nil.

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];
Run Code Online (Sandbox Code Playgroud)

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

小智 11

将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
    }
Run Code Online (Sandbox Code Playgroud)

这个链接帮助我:

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

  • 虽然此链接可能会回答这个问题,但最好在此处包含答案的基本部分并提供参考链接.如果链接的页面发生更改,则仅链接的答案可能会无效. (2认同)

And*_*uca 8

我也使用swift遇到过这个问题,但是1小时后,观察到我没有使用segue .destinationViewController ,而是使用了发送器 .destinationViewController,它搞砸了所有东西.你确定你使用的是segue而不是发件人吗?