0

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.

Rohan Vasishth
  • 417
  • 6
  • 19

2 Answers2

1

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.

leavesster
  • 99
  • 1
  • 4
  • so the navigation controller is nil. so i decided to create one. however, even when i did that the push still didnt work – Rohan Vasishth Jul 01 '17 at 02:24
  • @RohanVasishth So the navigation controller is nil? It seems you not call the right vcC, you just create a new vcC which is not visible. you can create a property call vcCreference, so you can use vcA.vcCreference showViewChatController methods – leavesster Jul 01 '17 at 09:30
  • @RohanVasishth you can see this [passing-data-between-view-controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?rq=1) to pass data from one view controller to another – leavesster Jul 01 '17 at 09:34
  • The post you showed me does everything through storyboards. Please note that I am doing all of this programmatically. Threfore could you just show me a post or show me yourself how to create a property call such as vcCreference. – Rohan Vasishth Jul 01 '17 at 14:40
  • @RohanVasishth could give me a demo on github in your case, so that I can should how to pass vcA data to vcC, not vcB, the simple way is pass data to vcB, then pass data from vcB to vcC. You can also use luckyShubhra's way——just post notification, put all data in notification.userInfo[Notification pass data](https://stackoverflow.com/questions/30328452/how-to-pass-multiple-values-with-a-notification-in-swift) , this link may help you – leavesster Jul 01 '17 at 15:45
  • I just figured it out. You were right. I used the notification stuff. thanks for all your help! – Rohan Vasishth Jul 01 '17 at 15:51
0

You can use Notification Pattern for the same. If you want to switch directly from vcA to VcD, fire notification in vcA before switching to vcD. Make vcB, vcC as listener to the same notification. When u directly switch from vcA to vcD, vcB and vcC will get notified if they already present. Handle the notification in vcB and vcC and hence dismissViewController or PopViewController as required. You can even pass data through from vcA directly to vcD using notification.

You can't push a view controller into a view dismissed, if it's dismissed it disappears so it's not logical to push another view, cause the parent view controller is deleted.

What you have to do is - vcA --> Present vcB --> Dismiss vcB --> Present vcC Push vcD on vcC > Dismiss vcC

You can do it with notifications. You can even accomplish the task using delegation pattern. Make vcB as delegate of vcC that notifies vcB when vcB dismisses then dismiss vcB and then just push vcD on vcC.

Hope it helps. Happy Coding!!

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19