2017-08-03 165 views
1

我有兩個視圖控制器:ViewControllerAViewControllerBViewControllerA被添加到一個容器視圖,它有按鈕。點擊按鈕時,ViewControllerB應取代ViewControllerA。我怎樣才能做到這一點?問題在於該按鈕位於ViewControllerB之內,而不在主視圖控制器中。在同一個容器視圖中從一個視圖控制器切換到另一個視圖

主要ViewController

class ViewController: UIViewController { 

    @IBOutlet weak var containerView: UIView! 
    weak var currentViewController: UIViewController? 

    override func viewDidLoad() { 
     self.currentViewController = self.storyboard?.instantiateViewController(withIdentifier: "Verify") 
     self.currentViewController!.view.translatesAutoresizingMaskIntoConstraints = false 
     self.addChildViewController(self.currentViewController!) 
     self.initializeFirstScreen(subView: self.currentViewController!.view, toView: self.containerView) 

     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    func initializeFirstScreen(subView:UIView, toView parentView:UIView){ 

     parentView.addSubview(subView) 

     var viewBindingDict = [String: AnyObject]() 
     viewBindingDict["subView"] = subView 
     parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[subView]|", options: [], metrics: nil, views: viewBindingDict)) 
     parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[subView]|", options: [], metrics: nil, views: viewBindingDict)) 
    } 

    func switchToValidate(){ 

     let newViewController = self.storyboard?.instantiateViewController(withIdentifier: "Validate") 

     //this line throws error 
     //fatal error: unexpectedly found nil while unwrapping an Optional value 
     newViewController!.view.translatesAutoresizingMaskIntoConstraints = false 
     self.addChildViewController(newViewController!) 
     initializeFirstScreen(subView: (newViewController?.view)!, toView: self.containerView) 

    } 
} 

ViewControllerA

class VerifyViewController: UIViewController { 

    let mainViewController = ViewController()  

    @IBAction func VerifyNumber(){ 

     self.switchScreens() 

    } 


    func switchScreens(){ 

     mainViewController.switchToValidate() 

    } 
} 

switchToValidate引發以下錯誤:

fatal error: unexpectedly found nil while unwrapping an Optional value

+0

我們需要一些代碼來幫助您 – pierreafranck

+0

試試這個,讓我知道。 self.currentViewController = self.storyboard?.instantiateViewController(withIdentifier:「Verify」)as! VerifyViewController self.currentViewController.mainViewController = self –

+0

這部分內容不清楚self.currentViewController.mainViewController = self currentViewController沒有mainViewController – kosas

回答

1

我掙扎有點理解你的代碼。

如果需要維護ViewControllerA中的按鈕,則應在其中注入主視圖控制器的引用。在ViewControllerA中,您正在創建ViewController的新實例。

因此,內ViewController,而不是做(你在這裏創建一個全新的控制器):

let viewController = ViewController() 

,你需要有這樣的事情:

weak var mainViewController: ViewController? 

viewDidLoad你可以注入在ViewControllerA之內屬性:

if let viewControllerA = 
    self.storyboard?.instantiateViewController(withIdentifier: "Validate") as? ViewControllerA { 
    viewControllerA.mainViewController = self 
    // other code here 
} 

現在,在ViewControllerA中,您可以參考主控制器,您可以切換到另一個控制器。

幾點注意事項:

  • super.viewDidLoad()應覆蓋viewDidLoad
  • 時,被稱爲第一請示,以應對自選喜歡if let(或guard)語法!。顯然除非是必要的。
相關問題