Segue.destinationViewController is zero when representing the modality of the UINavigationController

I have a UICollectionView , and when the user clicks on the cell, I present another view controller in the UINavigationController using the storyboard.

 - (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]; } 

Storyboard

Now my problem is that segue.desinationViewController returns nil (as the comment says in the code snippet).

Just for debugging, I changed the UINavigationController to another view controller, and I had no problems. I don’t know if the transition from modal to pressing will help, since the transition style will help, since it is impossible to press the UINavigationController (a failure occurs, which says that it is).

I cleaned up the project and build folder and restarted my computer (and therefore Xcode).

Here's what it looks like when the application starts:

Screenshothot of problem

When searching for similar problems, I did not find anything about this. Most of the other questions related to the properties set on the destination view controller to be zero (for example, this ).

I am using Xcode 5.1.1 and iOS 7.0 as a development goal.

Edit1

The same thing happens in all parts of my application (everywhere a UINavigationController appears modally). However, this only happens in some cases, but each time the segue.destinationViewController is still nil .

Edit2

I replaced the prepareForSegue code with this (by doing this manually):

 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]; 

And it works. So I think the problem is the storyboard somehow.

+6
source share
3 answers

I also ran into this problem using swift, but after 1 hour I noticed that instead of segue .destinationViewController, I used the sender .destinationViewController, which messed everything up. Are you sure you are using segue not sender?

+8
source

I had the same problem when embedding a viewcontroller inside a 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 } 

This link helps me:

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

+8
source

I ran into the same problem, and I think the problem is embedding the viewcontroller inside the NavigationController. As soon as I removed the navigation controller, prepareForSegue worked as expected, and segue.destinationViewController began to return the correct viewController class.

Just wanted to add this as information.

So my problem was segue.destinationViewController, but there was nothing in it to pass it to self.preboardingViewController:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "GoToPreBoarding" { self.preboardingViewController = segue.destinationViewController as? PreBoardingViewController self.preboardingViewController?.myShiftViewController = self } } 

So, in quick, I solved the problem in much the same way as yours, which caused the topviewController navigation manager to drop me a little. Here is my solution:

 let storyboard = UIStoryboard(name: "PreBoarding", bundle: nil) let navCon = storyboard.instantiateViewControllerWithIdentifier("PreBoardingNavigation") as! UINavigationController navCon.modalPresentationStyle = UIModalPresentationStyle.FormSheet self.preboardingViewController = navCon.topViewController as? PreBoardingViewController self.preboardingViewController?.myShiftViewController = self self.presentViewController(navCon, animated: true, completion: nil) 
0
source

Source: https://habr.com/ru/post/988654/


All Articles