简体   繁体   English

关闭一个ViewController并显示另一个ViewControlloer-IOS-Swift 3

[英]Dismiss a ViewController and present another ViewControlloer - IOS - Swift 3

I would like to dismiss a ViewController and present another ViewController. 我想解雇一个ViewController并提出另一个ViewController。 Here is my code: 这是我的代码:

MainVC: MainVC:

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

FirstViewController: 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: 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!" 而且我得到这个错误:“ FirstViewController上的SecondViewController,其视图不在窗口层次结构中!”

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 所以基本上我希望SecondViewController成为SecondViewController的废料和MainVC didLoad之间的一种插页式广告

A basic scheme: 基本方案:

FirstViewController->SecondViewController->MainVC 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 显示插页式广告首先打开mainviewcontroller和onViewDidAppear的正确方法显示您的广告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 和您的实时ViewController在firstviewController之前

class LiveViewController: UIViewController, LiveProtocol {

    ...

    internal func onFirstViewControllerDismissed() {

        // present your interstitial 

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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