2017-06-06 61 views
1

好的,希望我能儘可能清楚地解釋這一點。從一個特定的childViewControllers類調用方法

我有一個父ViewController有一個ContainerView,這將包含不同的viewControllers /類。我改變了這個容器的內容在運行時是這樣的:

let newController = (storyboard?.instantiateViewController(withIdentifier: classIdentifierFromStoryboard))! as UIViewController 
    let oldController = childViewControllers.last! as UIViewController 

    oldController.willMove(toParentViewController: nil) 
    addChildViewController(newController) 
    newController.view.frame = oldController.view.frame 

    transition(from: oldController, to: newController, duration: 0, options: .transitionCrossDissolve, animations:{() -> Void in 
     }, completion: { (finished) -> Void in 

    }) 

    oldController.removeFromParentViewController() 
    newController.didMove(toParentViewController: self) 

因此,例如,電流控制器,顯示在UI是firstVC,這有點像

class firstVC { 
    func removeAnnotations() { 
     //something that removes annotation from MapVC 
    } 
} 

在某事件在父控制器上,我如何訪問firstVC的實例,以便能夠調用removeAnnotations?截至目前,我得到零,在父控制器firstVC.removeAnnotations上使用此,可能這會調用該類的另一個實例。任何幫助將是偉大的!希望我的解釋有道理。謝謝!

回答

0

你可以跑下來的孩子視圖控制器是這樣的:

for case let vc as firstVC in self.childViewControllers { 
    vc.removeAnnotations() 
} 

這將耗盡所有的子視圖控制器,是類型firstVC和執行removeAnnotations方法

0

在此代碼是孩子鑑於第一控制器ProfileViewController

創建子視圖控制器的一個目的是這樣

var objectProfile:ProfileViewController! 

然後viewDidLoad方法

override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 

     let storyboard = UIStoryboard(name: "Main", bundle: nil) 

     objectProfile = storyboard.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController 
     objectProfile.view.frame = CGRect(x: -(self.view.frame.size.width - 40), y: 0, width: self.view.frame.size.width - 40, height: self.view.frame.size.height) 
     self.view.addSubview(objectProfile.view) 
     self.navigationController?.didMove(toParentViewController: objectProfile) 

     objectProfile.view.isHidden = true 

    } 

那麼這裏按鈕單擊方法代碼

@IBAction func btnProfilePressed(_ sender: UIButton) { 

     UIView.animate(withDuration: 0.3) { 

      self.objectProfile.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width - 40, height: self.view.frame.size.height) 


     } 

    } 

隨後的touchesBegan

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 

     objectProfile.view.isHidden = false 

     UIView.animate(withDuration: 0.3) { 

      self.objectProfile.view.frame = CGRect(x: -(self.view.frame.size.width - 40), y: 0, width: self.view.frame.size.width - 40, height: self.view.frame.size.height) 


     } 

    } 

一套完整的驗證碼出來的說就是

enter image description here enter image description here

+0

這不是回答原來的問題。這顯示瞭如何添加子視圖控制器,但在原始問題中,兩個子視圖控制器之間的轉換已經完成,所需的是如何在轉換後訪問新的子視圖控制器。 –

相關問題