[英]iOS Swift 4: ist it more efficient to dismiss or present a ViewController
[英]Dismiss a ViewController and present another ViewControlloer - IOS - Swift 3
我想解雇一個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
您的任何建議都將不勝感激。 謝謝!
您不應使用靜態函數來創建視圖控制器。 顯示插頁式廣告首先打開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
}
協議(委托)樣本
// give the name you want
protocol LiveProtocol: class {
func onFirstViewControllerDismissed()
}
和您的實時ViewController在firstviewController之前
class LiveViewController: UIViewController, LiveProtocol {
...
internal func onFirstViewControllerDismissed() {
// present your interstitial
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.