Reputation:
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)
})
Upvotes: 3
Views: 297
Reputation: 23596
The reason nothing is happening is because self.presentingViewController?
is nil.
If instead, you were using self.presentingViewController!
, there would be an error stating Unexpectedly found nil while unwrapping optional value, which should always be avoided.
You are correct in using self.presentingViewController?
instead of self.presentingViewController!
, as it is much safer, but in order to fix this, you must make sure that self.presentingViewController?
is not nil.
let presenting = self.presentingViewController
self.dismissViewControllerAnimated(true, completion: {() -> Void in
presenting?.presentViewController(controller, animated: true, completion: nil)
})
Upvotes: 0
Reputation: 52163
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.
Upvotes: 2