2017-03-09 94 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因此委托模式 –

相关问题