developer tip

내비게이션 컨트롤러를 통한 신속한 데이터 전달

copycodes 2020. 11. 26. 18:45
반응형

내비게이션 컨트롤러를 통한 신속한 데이터 전달


내 segues 중 하나가 뷰 컨트롤러에서 테이블 뷰 컨트롤러로 전환됩니다. 둘 사이에 배열을 전달하고 싶지만 tableview 컨트롤러 앞에 탐색 컨트롤러가 있으면 배열을 전달하는 방법을 알 수 없습니다. 탐색 컨트롤러를 통해 테이블 ​​뷰 컨트롤러로 데이터를 어떻게 전달합니까?


prepareForSegue원하는 tableview의 값을 재정의 하고 설정하십시오. UINavigation viewControllers속성 에서 tableview를 가져올 수 있습니다 .

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!){

   let navVC = segue.destinationViewController as UINavigationController

   let tableVC = navVC.viewControllers.first as YourTableViewControllerClass

   tableVC.yourTableViewArray = localArrayValue   
}

Swift 3의 경우 :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

   let navVC = segue.destination as? UINavigationController

   let tableVC = navVC?.viewControllers.first as! YourTableViewControllerClass

   tableVC.yourTableViewArray = localArrayValue   
}

@Tommy Devoy의 대답은 정확하지만 스위프트 3에서도 똑같은 것이 있습니다.

Swift 3 업데이트

  // MARK: - Navigation

//In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.


    if segue.identifier == "yourSegueIdentifier"  {

        if let navController = segue.destination as? UINavigationController {

            if let chidVC = navController.topViewController as? YourViewController {
                 //TODO: access here chid VC  like childVC.yourTableViewArray = localArrayValue 


            }

        }

    }

}

Horatio의 솔루션 에이 오류가 발생 했습니다.

ERROR: Value of type 'UINavigationController' has no member 'yourTableViewArray'

So this might help others like me looking for a code only solution. I wanted to redirect to a Navigation controller, yet pass data to root view controller within that Nav Controller.

if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourNavigationControllerID") as? UINavigationController,
   let yourViewController = controller.viewControllers.first as? YourViewController {
        yourViewController.yourVariableName = "value"
        self.window?.rootViewController = controller  // if presented from AppDelegate
        // present(controller, animated: true, completion: nil) // if presented from ViewController
}

Tommy's solution requires you to set up the Segue in Storyboard.

Here is a code only solution:

func functionToPassAsAction() {
    var controller: UINavigationController
    controller = self.storyboard?.instantiateViewControllerWithIdentifier("NavigationVCIdentifierFromStoryboard") as! UINavigationController 
    controller.yourTableViewArray = localArrayValue
    self.presentViewController(controller, animated: true, completion: nil)
}

SWIFT 3 (tested solution) - Passing data between VC - Segue with NavigationController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        let navigationContoller = segue.destination as! UINavigationController

        let receiverViewController = navigationContoller?.topViewController as ReceiverViewController

        receiverViewController.reveivedObject = sentObject 
    }

FIXED syntax for SWIFT 3

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let navVC = segue.destination as! UINavigationController
        let tableVC = navVC.viewControllers.first as! YourTableViewControllerClass

        tableVC.yourTableViewArray = localArrayValue
    }

참고URL : https://stackoverflow.com/questions/25369412/swift-pass-data-through-navigation-controller

반응형