简体   繁体   中英

Dismiss a ViewController and present another ViewControlloer - IOS - Swift 3

I would like to dismiss a ViewController and present another ViewController. Here is my code:

MainVC:

@IBAction func loadFirstVCClicked(_ sender: UIButton)
{
    self.present(FirstViewController.loadVC()
}

FirstViewController:

static func load() -> FirstViewController
{
    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController
    return vc
}
@IBAction func exitClicked(_ sender: UIButton)
{
    self.dismiss(animated: true, completion: {
        self.present(SecondViewController.loadVC()
    })
}

SecondViewController:

static func loadVC() -> SecondViewController
{
    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
    return vc
}

@IBAction func exitClicked(_ sender: UIButton)
{
    self.dismiss(animated: true, completion: {
        //present MainVC - second VC should work as an interstitial ad
    })
}

And I get this error: "SecondViewController on FirstViewController whose view is not in the window hierarchy!"

And it doesn't get displayed. So basically I want the SecondViewController to be a sort of interstitial ad between the dissmisal of SecondViewController and the MainVC didLoad

A basic scheme:

FirstViewController->SecondViewController->MainVC

Any suggestions from you would really be appreciated. Thank you!

You should not use static functions to create viewcontrollers. The proper way of displaying interstitial first open mainviewcontroller and onViewDidAppear show your ad viewcontroller

@IBAction func loadFirstVCClicked(_ sender: UIButton)
{
    let firstViewController = self.storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController
    self.present(firstViewController)
}

@IBAction func loadMainVCClicked(_ sender: UIButton)
{
    let mainViewController = self.storyboard?.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
    self.present(firstViewController)
}

class MainViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        // there you can present your interstitial viewcontroller
    }

}

class InterstitialViewController: UIViewController {

    // when user press close or after a time limit dismiss 

}

A protocol (delegate) sample

// give the name you want
protocol LiveProtocol: class {

    func onFirstViewControllerDismissed()

}

And your live viewcontroller before firstviewcontroller

class LiveViewController: UIViewController, LiveProtocol {

    ...

    internal func onFirstViewControllerDismissed() {

        // present your interstitial 

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM