我想解散ViewController并呈现另一个ViewController。这是我的代码:
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
})
}
我收到此错误:" FirstViewController上的SecondViewController,其视图不在窗口层次结构中!"
它并没有显示出来。所以基本上我希望SecondViewController成为SecondViewController和MainVC didLoad之间的一种插页式广告
基本方案:
FirstViewController-> SecondViewController-> MainVC
您的任何建议都会非常感激。谢谢!
答案 0 :(得分:0)
您不应该使用静态函数来创建viewcontrollers。显示插页式首页打开主视图控件和onViewDidAppear的正确方法是显示广告视图控件
@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
}
协议(代表)样本
// give the name you want
protocol LiveProtocol: class {
func onFirstViewControllerDismissed()
}
在firstviewcontroller之前你的实时viewcontroller
class LiveViewController: UIViewController, LiveProtocol {
...
internal func onFirstViewControllerDismissed() {
// present your interstitial
}
}