2017-06-06 4 views
1

申し訳ありませんが、これを可能な限り明確に説明できることを願っています。特定のchildViewControllersクラスからの呼び出しメソッド

私はContainerViewを持つ親ViewControllerを持っていますが、これは異なるviewControllers/Classを含みます。私はこのように、実行時にこのコンテナの内容を変更しています:

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上で示した電流コントローラはちょっと、特定のイベントで

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

のように見えるfirstVC、あります親コントローラーで、firstVCのインスタンスにアクセスして、removeAnnotationsを呼び出せるようにするにはどうすればよいですか?今度は親コントローラfirstVC.removeAnnotationsでこれを使用していますが、これはクラスの別のインスタンスを呼び出すことが想定されています。どんな助けも素晴らしいだろう!私の説明が理にかなっているといいですね。ありがとう!

答えて

0

あなたはこのように、子View Controllerをダウンを実行できます。これは、このコードでremoveAnnotations方法

0

をタイプfirstVCのあるすべての子View Controllerをダウンを実行し、実行します

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

子です最初のビューコントローラ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

これは元の質問には答えていません。これは、子ビューコントローラを追加する方法を示していますが、元の質問では、2つの子ビューコントローラ間の移行は、移行後に新しい子ビューコントローラにアクセスする方法が必要でした。 –

関連する問題