0
votes

I have three view controllers

  • ViewControllerA
  • ViewControllerB
  • ViewControllerC

This is a scenario I'm trying to solve

  • ViewControllerA pushes ViewControllerB on navigation hierarchy
  • ViewControllerB presents ViewControllerC as modal, with close delegate

  • ViewControllerC close button is pressed, close delegate is sent and ViewControllerC is dismissed

  • ViewControllerB receives the close delegate, and tries to dismiss itself so ViewControllerA is displayed

For some reason ViewControllerB is not being dismissed. If I press the cancel button in ViewControllerB then it can be dismissed.

Why can’t consecutive dismiss’ be done?

ViewControllerC

@IBAction func closeClick(sender: AnyObject) {
    self.closeDelegate?.didClose()
    self.dismissViewControllerAnimated(true, completion: nil)
}

ViewControllerB

func didClose() {
        print("did close") // gets called
        self.dismissViewControllerAnimated(false, completion: nil) // no dismiss
        //self.navigationController?.popViewControllerAnimated(false)
}

@IBAction func cancelClick(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil) // works
}
1
You should call dismissViewController from ViewControllerB, not ViewControllerC. From your description it sounds like you called pushViewControllerAnimated to present ViewControllerB. If that is true you need to call popViewControllerAnimated to get back to ViewControllerA.Lev Landau
Because at the point you call your didClose delegate, view controller C hasn't yet dismissed itself. If you are using storyboard then define an unwind segue to view controller A and trigger that from view controller c, otherwise I would suggest that you try calling the didClose delegate after the dismiss or have view controller b dismiss C in the delegate methodPaulw11
@Paulw11 I ended up dismissing view C in from controller b as suggested. If you put this as an answer I can mark as 'answered'. Although the transition isn't as smooth as you will see a flash of view B before it displays view A.fes

1 Answers

1
votes

You said that you are pushing view controller B onto your navigation controller. Dismissing view controller B is not possible because the navigation controller that contains it is the root view controller.

You will need to pop back to the navigation controller's root view controller, so your delegate method implementation on view controller A should execute a pop and not a dismiss.