Pangu
Pangu

Reputation: 3819

How to properly dismiss the presented ViewController and go back to the parent ViewController?

I'm facing an issue that I will describe and have found some similar questions, but I don't believe pertains to my issue.

I have a UITabBarController with 2 tabs for VC1 and VC2. VC1 segues to VC4. VC2 segues to VC3 and VC3 segues to VC4. VC4 contains an MPMoviePlayerViewController instance, as depicted below:

             - - - - - -> VC1 \
TAB BAR VC /                    - - -> VC4
           \                   /
             -> VC2 -> VC3 -> /

I have a Notification that listens to when the Video finishes, and dismiss VC4, and go back to whichever parent VC presented VC4, i.e. if VC1 presented VC4, then upon dismissal of VC4, should go back to VC1. Likewise, if VC3 presented VC4, then upon dismissal of VC4, should go back to VC3.

In VC4:

override func viewDidLoad()
{
    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "moviePlayerPlayBackDidFinish:",
        name: MPMoviePlayerPlaybackDidFinishNotification,
        object: player?.moviePlayer)
}

func moviePlayerPlayBackDidFinish(notification: NSNotification)
{
    NSNotificationCenter.defaultCenter().removeObserver(
        self,
        name: MPMoviePlayerPlaybackDidFinishNotification,
        object: notification.object
    )

    player!.view.removeFromSuperview()

    self.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)
}

I don't think I'm using the code properly because self.presentedViewController?.dismissViewControllerAnimated(true, completion: nil) stays at VC4.

I found some similar questions:

  1. How to dismiss the current ViewController and go to another View in Swift
  2. How to dismiss VIewController iOS Swift
  3. modal View controllers - how to display and dismiss

However, the sequence segues are different, therefore I can't follow the suggestions.

How can I achieve this? Thanks

UPDATE:

The following code in moviePlayerPlayBackDidFinish properly dismisses VC4 and back to parent VC1 if going from VC1 -> VC4:

self.dismissViewControllerAnimated(true, completion: nil);

However, VC4 does not dismiss when going from VC3 -> VC4.

Upvotes: 1

Views: 1831

Answers (1)

Pangu
Pangu

Reputation: 3819

Finally solved my issue by adding the additional code in moviePlayerPlayBackDidFinish:

self.navigationController?.popViewControllerAnimated(true)

After much research, I found the solution from here: dismissViewControllerAnimated() does not dismiss view controller

Upvotes: 1

Related Questions