Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismissing a UINavigationController and Presenting another UINavigationController

I have a two view controllers, ViewControllerA and ViewControllerB, embedded in a UINavigationController. ViewControllerA has a UICollectionView that displays images.

When the camera icon located at index 0 and the UICollectionViewCell is selected, it should dismiss ViewControllerA and display ViewControllerB. And also, there is a ViewController that presents ViewControllerA modally

This is what I've done, but it only dismiss ViewControllerA and nothing happens

let controller = self.storyboard?.instantiateViewControllerWithIdentifier("cameraViewController") as! UINavigationController
        self.dismissViewControllerAnimated(true, completion: { () -> Void in
            self.presentingViewController?.presentViewController(controller, animated: true, completion: nil)
        })

1 Answers

When view controller is dismissed, my guess is self is already deallocated so self.presentingViewController? becomes nil too. I would create a strong reference to presenting view controller right before dismissing action takes place:

let presentingViewController = self.presentingViewController
self.dismissViewControllerAnimated(true) { completed in
  if completed {
    presentingViewController?.presentViewController(controller, animated: true, completion: nil)
  }
}

Also, make sure self.presentingViewController is not nil in the first place.

like image 67
Ozgur Vatansever Avatar answered Aug 04 '26 10:08

Ozgur Vatansever