2
votes

I have a view controller that presents another view controller like so

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = .white

   let qrScannerViewController = QRScannerViewController()
   qrScannerViewController.presentedBy = self
   self.present(qrScannerViewController, animated:true, completion: nil)

    // Do any additional setup after loading the view.
}

qrScannerViewController (the presented view controller) then calls

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

which to my understanding calls the presenting view controllers dismiss function anyway.

Problem is, once the presented view controller has been dismissed, the presenting view controller's viewDidLoad gets called again, meaning the view controller is presented again.

Any ideas how to get around this?

Even if I use delegation the presenting view controller's viewDidLoad gets called again

Thanks

The presenting view controller is defined in a UITabController:

let qrPlaceholderViewController = QRPlaceholderViewController()
let controllers = [restaurantNavController,favouritesViewController, qrPlaceholderViewController, profileViewController]
self.viewControllers = controllers
1
Check properly may be you have some where else also code for self.present.Nirav D
viewDidLoad isn't supposed to be called again, perhaps you've repeated the code in viewWillAppear?jjatie
@jjatie I do not have viewWillAppear defined? The controller is the contained inside a tab, may this be why?mwild
Is QRScannerViewController set up in a storyboard?jjatie
@Idan The question here is how to prevent the code from being called every time the initial VC appears.jjatie

1 Answers

1
votes

Ok so the problem here was ARC doing its job.

When the presenting view controller presented the other view controller, ARC was unloading the presenting controller. This meant that when the presented view controller was dismissed, the presenting one was reinstantiated, hence forcing the viewDidLoad method to get called again

Solution:

A few solutions are available:

First of all I just stored a flag in a helper that I could check in the viewDidLoad method to see if it had already been loaded before and if it had, dont present the view controller again

Alternatively, I changed to once a qr code had been scanned, call a function in the presented view controllers delegate (the presenting controller) that navigated to the view that I wanted, therefore skipping the issue of the viewDidLoad being executed again.