Presenting view controllers on detached view controllers is discouraged (with SWRevealViewController)

207 views Asked by At

I am changing the RootViewController of SwRevealViewController

My code:

let next = self.storyboard?.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
    next.loadView()
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = mainStoryboard.instantiateViewController(withIdentifier: "MessageViewController") as! MessageViewController
    UIApplication.shared.keyWindow?.rootViewController = viewController;

    self.present(next, animated: true, completion: nil)

But I get this error:

>Presenting view controllers on detached view controllers is discouraged <ios.MesajDetayViewController: 0x7f8718d646f0>.

How to solve?

2

There are 2 answers

0
Tolga İskender On BEST ANSWER

solution

    let sw = storyboard?.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
self.view.window?.rootViewController = sw

let mainStroyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let desController = mainStroyBoard.instantiateViewController(withIdentifier: "MessageViewController") as! MessageViewController
let newFrontViewController = UINavigationController.init(rootViewController:desController)
sw.pushFrontViewController(newFrontViewController, animated: true)
0
JuicyFruit On

if you want to change SWRevealViewContoller front view controller, you should use

let newFronViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "YourID") as! YourClass
self.revealViewController().setFront(newFronViewController, animated: false)

I can update my answer depending on where you would like to call this.