2017-05-08 64 views
1

我有一個viewController包含segmentedControl。我有一個位於segmentedControl的VCA和VCB。當我點擊第二段VCB出現。現在我正在從VCB推送另一個ViewController。但是從viewController返回時,正在調用VCA的viewDidAppear。這對我來說很奇怪。因爲用戶在VCB上,所以爲什麼調用VCA的viewWillAppearviewDidAppear?這裏有一個圖來解釋更viewDidAppear被稱爲strangley

enter image description here

這是怎麼了加入viewControllers到segmentedControl

func switchToViewController(viewController: UIViewController, selectedIndex: Int) { 

    viewController.removeFromParentViewController() 
    viewController.view.removeFromSuperview() 

    addChildViewController(viewController) 
    viewController.view.translatesAutoresizingMaskIntoConstraints = false 
    self.view.addSubview(viewController.view) 

    // Setting constraints of the container view 
    NSLayoutConstraint.activate([ 
    viewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0), 
     viewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0), 
     viewController.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 50), 
     viewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0) 
     ]) 

    viewController.didMove(toParentViewController: self) 
} 

我只是無法理解的行爲。所以請指導我。

+0

是包含分段控制和VCA相同的viewcontroller嗎? – Priyal

+0

不,它們不一樣。包含segmentedControl的viewController是一個不同的VC讓我們稱之爲VCSegmentedControl –

+0

爲什麼你要刪除'viewController'並添加相同的'viewController'? –

回答

2

你永遠去除當前視圖控制器,並從層次結構中的視圖...

您需要跟蹤它的當前顯示VC /視圖 - 或許還有一個currentVC變量,你的功能應該是這樣的:

func switchToViewController(viewController: UIViewController, selectedIndex: Int) { 

    // remove current ViewController from VC hierarchy 
    currentVC.removeFromParentViewController() 

    // remove current VC.View from View hierarchy 
    currentVC.view.removeFromSuperview() 

    // the "incoming" ViewController becomes the "current" ViewController 
    currentVC = viewController 

    addChildViewController(viewController) 
    viewController.view.translatesAutoresizingMaskIntoConstraints = false 
    self.view.addSubview(viewController.view) 

    // Setting constraints of the container view 
    NSLayoutConstraint.activate([ 
    viewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0), 
     viewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0), 
     viewController.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 50), 
     viewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0) 
     ]) 

    viewController.didMove(toParentViewController: self) 
} 
+0

非常感謝您的解釋: - ) –

1

在這裏,你可以通過使用containerView來實現這一點。 ContainerView是一個正常的UIView子類。你的用戶界面會是這樣的。你將有一個baseViewController你將在baseViewController中有segmentControlcontainerView視圖。

enter image description here

假設你有兩個視圖控制器即viewController1viewController2。您可以將這些viewControllers作爲childViewController添加到此類containerView,如下所示。

import UIKit 

class BaseViewController: UIViewController { 

    @IBOutlet weak var typeSegment: UISegmentedControl! 
    @IBOutlet weak var containerView: UIView! 

    var viewController1: UIViewController? 
    var viewController2: UIViewController? 


    // MARK: - Action method. 

    @IBAction func segmentIndexChanged(_ sender: Any) { 

     let selectedIndex = typeSegment.selectedSegmentIndex 

     switch selectedIndex { 
     case 0: 
      addVC1() 
     case 1: 
      addVC2() 
     default: 
      break 
     } 

    } 

func rectForChildVC() -> CGRect { 

    let rect = CGRect(x: containerView.frame.origin.x , y: containerView.frame.origin.y, width: containerView.frame.size.width, height: containerView.frame.size.height) 
    return rect 

} 

func addVC1() { 

    removeVC2() 
    let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil) 
    viewController1 = storyboard.instantiateViewController(withIdentifier: "Viewcontroller1Identifier") // Create you first view controller instance here. 
    viewController1?.view.frame = rectForChildVC() 
    addChildViewController(viewController1!) 
    view.addSubview((viewController1?.view)!) 
    viewController1?.didMove(toParentViewController: self) 
    view.layoutIfNeeded() 

} 

func addVC2() { 

    removeVC1() 
    let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil) 
    viewController2 = storyboard.instantiateViewController(withIdentifier: "Viewcontroller2Identifier") // Create you second view controller instance here. 
    viewController2?.view.frame = rectForChildVC() 
    addChildViewController(viewController2!) 
    view.addSubview((viewController2?.view)!) 
    viewController2?.didMove(toParentViewController: self) 
    view.layoutIfNeeded() 

} 

func removeVC1() { // Remove first view controller. 

    if let viewController = viewController1 { 
     viewController.didMove(toParentViewController: nil) 
     viewController.view.removeFromSuperview() 
     viewController.removeFromParentViewController() 
    } 

} 

func removeVC2() { // Remove second view controller 

    if let viewController = viewController2 { 
     viewController.didMove(toParentViewController: nil) 
     viewController.view.removeFromSuperview() 
     viewController.removeFromParentViewController() 
    } 

} 

} 

謝謝。

+0

感謝您的解決方案,但我不想改變我的實施... :) –