Xcode 12.0 iOS 13+ UIViewController() problem with viewWillDisappear()

621 Views Asked by At

I have a simple flash card app I wrote for a friend of mine. I'm a hobby-est at best. It's essentially Tinder that you can flip the card over for a dead language. Everything has been working great up until the iOS 13 update with how Apple redid the Storyboards for flexibility.

My problem is my elegant solution to save userdefaults when exiting a view is no longer being called. This "carddeck" view controller is called when a button on another screen is pressed. To exit this same view controller before you would press a button that was tied to an "action segue - show" (not @IBAction) that would bring the view back to the "mainview" view controller. I tried the same action segue with "present modally." but no dice.

class CardDeckViewController: UIViewController {

override func viewDidLoad() {
        super.viewDidLoad()
        // called!
}

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        // called!
}

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(true)
        // not called when "return" on this screen is pressed; 
        // however, it still returns to the main screen it just doesn't save the user's score
}

Any help would be appreciated. My hope is there is an easier fix than redoing the entire storyboard. I see about UIModalPresentFullScreen. I'm no Swift guru so at this point I thought I would reach out to the pros. Hopefully this is too time consuming of a fix. Luckily I think the other views are not affected by this other than one more.

Thanks for reading!

2

There are 2 best solutions below

3
On

A couple of things you might try:

  1. It sounds like you're presenting the "main view" view controller again. Hard to tell without seeing your code but I think you should probably be dismissing "CardDeck" view controller in order to get back to your "main view" view controller.

  2. Have a look at UIAdaptivePresentationControllerDelegate. This stackoverflow post covers the change and iOS 13 solution.

  3. I had some similar issues with iOS 12 view controller lifecycle behavior being changed in iOS 13.

I suggest considering NotificationCenter if nothing else works. You broadcast from your current view controller and someone like your AppDelegate can receive the message and act on this. It's not always the best tool for the job but it can help in certain situations.

  1. Not related to your question but your calls to super super.viewWillAppear(true) should probably be super.viewWillAppear(animated) so you're not disregarding the method argument.
0
On

The answer was at the bottom of this question:

Presenting modal in iOS 13 fullscreen

I just had to redefine the storyboard UI property from Automatic to Full Screen.