gpt4 book ai didi

ios - 关闭一个 ViewController 并呈现另一个 ViewController - IOS - Swift 3

转载 作者:行者123 更新时间:2023-11-30 12:37:24 25 4
gpt4 key购买 nike

我想关闭一个 ViewController 并呈现另一个 ViewController。这是我的代码:

主VC:

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

第一个 View Controller :

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()
})
}

第二个 View Controller :

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
})
}

我收到此错误:“FirstViewController 上的 SecondViewController 其 View 不在窗口层次结构中!”

并且它不会显示。所以基本上我希望 SecondViewController 成为 SecondViewController 和 MainVC didLoad 之间的一种插页式广告

基本方案:

FirstViewController->SecondViewController->MainVC

如果您提出任何建议,我们将不胜感激。谢谢!

最佳答案

您不应使用静态函数来创建 View Controller 。显示插页式广告的正确方法首先打开 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

}

协议(protocol)(委托(delegate))示例

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

func onFirstViewControllerDismissed()

}

以及你的实时 View Controller 在firstviewcontroller之前

class LiveViewController: UIViewController, LiveProtocol {

...

internal func onFirstViewControllerDismissed() {

// present your interstitial

}

}

关于ios - 关闭一个 ViewController 并呈现另一个 ViewController - IOS - Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42705302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com