2017-03-09 112 views
0

我想解僱一個ViewController並提出另一個ViewController。這裏是我的代碼:關閉一個ViewController,並提出另一個ViewController - IOS - Swift 3

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

而且我得到這個錯誤:「SecondViewController上FirstViewController,其觀點是不在窗口層次!「

而且它不顯示。所以基本上我想要的SecondViewController是SecondViewController的dissmisal和MainVC didLoad

基本方案之間的一種插頁式廣告:從

FirstViewController-> SecondViewController-> MainVC

任何建議,你會真的很感激。謝謝!

+0

您正在嘗試從已經被解僱的FirstViewController呈現。你應該從MainVC出席。 – orxelm

+0

我會去使用一個由MainVC實現的代理,當你想關閉它時由SecondVC調用它。然後MainVC駁回SecondVC並呈現FirstVC –

回答

0

您不應該使用靜態函數來創建視圖控制器。顯示間質性首開mainviewcontroller和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() 

} 

和你住的ViewController之前firstviewcontroller

class LiveViewController: UIViewController, LiveProtocol { 

    ... 

    internal func onFirstViewControllerDismissed() { 

     // present your interstitial 

    } 

} 
+0

嗨,感謝您的建議,但我只想在FirstVC解散時顯示插頁式廣告,而不是MainVC出現時 –

+0

因此,您必須有另一個視圖控制器, FirstVC解散,您可以通過委託觸發它,並顯示插頁式廣告 – abdullahselek

+0

有沒有可能讓我看到?我是新來的ios因此委託模式 –

相關問題