6

I have an issue with an app I'm developing.

  • XCode version: 9.2 (9C40b)
  • Programming language: Swift 4
  • Target iOS version for the app: 11.2

Scenario: I have a mainVC (ViewController) which calls a modally presented secondaryVC. After doing a selection in the secondaryVC, I press a UIButton to go back to the mainVC through an unwind segue.

In the secondaryVC, transition is configured as "Cross Disolve" and Presentation, as "Over Current Context" to get see the previous view as background (background is configured with 50% opacity):

enter image description here

Symptom: in this scenario, when going back through the unwind segue, viewDidAppear never gets called. I need it to be called for further check functions to be executed. It gets called if instead of "Over Current Context", I set presentation as "Full Screen" but in that case, I can't see the previous view as a background.

Question: how can I make viewDidApper to be called keeping some transparency on the secondaryVC over the mainVC?

PS: Sorry if there's anything I've missed when writing this question; it's my first. I've searched through this and other forums and I haven't found an solution (or I haven't identified it).

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Markussen
  • 164
  • 1
  • 13
  • Maybe I'm missing something from your question, but in terms of `viewDidAppear`, I'd expect that it's called *once* per view controller, *when* they appear. Since it's an *unwind* segue of a modally presented VC, I'd expect that `viewWillDisappear` and then `viewDidDisappear` are about all that would be triggered, only for the second VC. –  Dec 31 '17 at 15:03
  • Actually, I didn't check if viewWillDisappear or viewDidDisappear were called in the secondaryVC. But the problem is that I want to raise an alert based on some post-checks, and those happen based on data in the mainVC so I considered moving this task (checking and alerting) to the secondary is not a good approach. Actually, I have 2 more secondaryVC's that bring data to mainVC. – Markussen Dec 31 '17 at 17:49
  • Sounds like you need to learn the **full** view life cycle and go from there. Here's *my* reference from here: https://stackoverflow.com/questions/5562938/looking-to-understand-the-ios-uiviewcontroller-lifecycle#12608364 –  Dec 31 '17 at 18:07

1 Answers1

8

The reason viewDidAppear is not being called is because the first view controller is never disappearing. If you use 'Over Current Context' then you can still see view controller one behind view controller two (assuming there are transparent sections as you have). So view controller one stays visible, never disappears and therefore when view controller two is shown neither viewWillDisappear or viewDidDisappear are called. Then when you unwind back the second view controller disappears but the fist doesn't appear so viewWillAppear and viewDidAppear aren't called.

If you use 'Full Screen' then the first view controller does disappear and hence all the functions fire.

If you need to do something when the second view controller disappears and you go back to the first view controller your could put it in the unwind function.

EDIT

This is a trick to perform some code like displaying an alert view controller from an unwind segue:

    DispatchQueue.main.async {
        let ac = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
        self.present(ac, animated: true, completion: nil)
    }

Basically what that has done is shove the required code back onto the main thread and it happens after the unwind segue is complete and you are back in the first view controller.

Upholder Of Truth
  • 4,643
  • 2
  • 13
  • 23
  • I was arriving to that conclusion, but it is somehow confusing for me that despite you can see the mainVC at the back of the secondaryVC, you're actually calling secondaryVC, so the one at the back (mainVC) disappears behind. To clarify on this: the reason why I arrived to this problem is that I need to perform some checks after coming from secondaryVC, and if there's an issue, I want to raise an alert on mainVC (as mainVC contains all the data). If I call the alert in the unwind segue, it will raise an error saying that I haven't returned from secondaryVC yet, so alert can't be raised. – Markussen Dec 31 '17 at 17:40
  • Well there is one trick you can do to overcome that problem and I will update my answer to show you. – Upholder Of Truth Dec 31 '17 at 18:28
  • Thanks a lot Upholder. I haven't tried it yet since I kept working on other functions of my app. For now, I'm saving this answer so I can come back an test it for later. PS: I upvoted but I don't have enough rep for that. – Markussen Jan 04 '18 at 12:06