Search code examples
iosswiftuiviewcontrollerpushviewcontrollerdismiss

Dismiss Two View Controllers and then Push a View Controller - Swift


I have 4 view controllers I am dealing with in this question. For simplictiy sake I will refer to them as vcA, vcB, vcC, and vcD.

Overall what I want to do is dismiss two view controllers and then push the third view controller to the 4th view controller. Essentially this is how the path would go: vcA(dismissed) -> vcB(dismissed) -> vcC pushes to vcD(final destination).

Here is my current code for the process. Please note I am able to dismiss the first two view controllers but I can't figure out how to push the third view controller. Anyways here is my code:

CODE FOR vcA:

@IBAction func createGroupButtonTapped(_ sender: Any) {
    if groupNameTextField.text == "" {
        ProgressHUD.showError("Please enter a group title")
    } else {

        self.presentingViewController?.dismiss(animated: false, completion: nil)

        NewConversationController().showDisplayConversationsController(title: self.groupNameTextField.text!, groupThumbnail: self.groupImageView, members: CreateConversationPopOver.groupMembers)

        self.presentingViewController?.dismiss(animated: true, completion: nil)

    }
}

In the code above I dismiss the first two view controllers and also call a function in vcB. This function in vcB calls a function in vcC. Here is the code for the functions:

FUNCTION IN vcB:

func showDisplayConversationsController(title: String, groupThumbnail: UIImageView, members: Array<String>) {
    dismiss(animated: true, completion: nil)
    DisplayConversationsController().showViewChatController(title: title, groupThumbnail: groupThumbnail, members: members)
}

FUNCTION IN vcC:

func showViewChatController(title: String, groupThumbnail: UIImageView, members: Array<String>) {
    let viewChatController = ViewChatController(collectionViewLayout: UICollectionViewFlowLayout())
    viewChatController.hidesBottomBarWhenPushed = true

    viewChatController.groupThumbnail = groupThumbnail
    viewChatController.groupMembers = members
    viewChatController.groupTitle = title

    navigationController?.pushViewController(viewChatController, animated: true)
}

The problem occurs in VCC where the view controller never pushes to the final destination otherwise known as vcD. I have added breakpoints within the function above and they are all called. So the code is being run.

I don't know why the view controller won't push because even after numerous testing, all my functions are being called.

Any help would be appreciated. Thanks!

PS: In case you are wondering why I want to dismiss views and present them this way, it is because I need to pass data through these views into a chat log controller.


Solution

  • first.
    check vcC's navigationController, it may be nil.
    It seems you call Class methods NewConversationController DisplayConversationsController, (not familiar with swift, or these two are ) not instance methods. Maybe it not the right vcC, just a new vcC?

    PS: It is quite strange that you vcA.presentingViewController dismiss twice and according to what you say, It just dismiss fine. if you can describe how you present vcA, vcB or give a demo to show how it work, I think it may be helpful.