I have an iPad app that has 3 viewControllers. vcMain, vcMenu, vcInfo
- vcMenu and vcInfo are custom sized windows
- vcMain uses a segue to presentModally the vcMenu
- The vcMenu has an info buttons that uses a segue to presentModally the vcInfo
After years, this no longer works as the vcMenu no longer appears to be the topmost window.
'Attempt to present vcInfo on vcMenu (from vcMenu whose view is not in the window hierarchy.'
I can bypass this issue when setting isModalInPresentation to true when vcMenu appears, but I do not want to change the behavior of the vcMenu by having to add a 'Close' button Also setting isModalInPresentation to true in the info button function before calling performSegue does not work. It also does not work if I add a delay inside the function (ie perform a #selector).
- Any suggestions?
- Is there a new setting somewhere that I am missing?
Here is the code...
import UIKit
class vcMain: UIViewController {
// all functions perfomed in storyboard
// a button uses a segue to open vcMenu with
// Kind: presentModally
override func viewDidLoad() { super.viewDidLoad() }
}
class vcMenu: UIViewController {
let ID_POP_UP = "id1"
@IBAction func btnInfo(_ sender: UIButton) {
isModalInPresentation = true // get error
// performSegue(withIdentifier: ID_POP_UP, sender: nil)
perform(#selector(presentExampleController), with: nil, afterDelay: 2)
}
@objc private func presentExampleController() {
// get error even if add a 2s delay
performSegue(withIdentifier: ID_POP_UP, sender: nil)
}
@IBAction func btnBack(_ sender: UIButton) {
isModalInPresentation = false
self.dismiss(animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let segueId = segue.identifier else { return }
if segueId == ID_POP_UP { print("ID_POP_UP: \(ID_POP_UP)") } // Just checking
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//isModalInPresentation = true // no error
}
}
class vcInfo: UIViewController {
override func viewDidDisappear(_ animated: Bool) {
if let main = self.parent { main.isModalInPresentation = false } // testing
super.viewDidDisappear(animated)
}
@IBAction func btnCloseAll(_ sender: UIButton) {
self.view.window!.rootViewController?.dismiss(animated: true, completion: nil) // close all popovers
}
}