简体   繁体   中英

How to pass data from View Controller to Container View in swift

This is my screens....

哦

I want to pass data from Offline View Controller to InstantVC. I don't know how to do that. Basically, I have segmented Controller. When user tab Instant it show the segmented view controller and hide the Schedule view controller. And pass data according to selected segmented.

Here is the Offline View controller to pass data


                switch response.result {

                case .success:
                    if let result = response.data {

                        do {

                            let resultIs = try JSONDecoder().decode(GetActiveOrderModel.self, from:result)
                            print("Massage:  \(resultIs.state)")
                            if let results = resultIs.orderData{
                                let storyboard = UIStoryboard(name: "Rider", bundle: nil)
                                let vc = storyboard.instantiateViewController(withIdentifier: "instantVC") as! InstantVC


                                print(" Order ID is  \(results.orderId)")
                                vc.arryOfOrder.append(results.orderId!)
//navigationController?.pushViewController(vc, animated: true)

                            }
                        } catch {
                            print(error)

                        }
                    }

                case .failure(let error):
                    print(error)


                }

And here is Instant view controller that restive data from Offline VC

var arryOfOrder = [Int]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
       
        print("---------->>>>>>>>> \(arryOfOrder)")
        
    }
    
    
    // MARK: - Custom Functions
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arryOfOrder.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! CellForInstantVC
        
        
        
        cell.orderNum.text = "\(arryOfOrder[indexPath.row])"
        
        return cell
        
        
    }
    
    func addOrderNumber(orderNumber : Int){
        
        arryOfOrder.append(orderNumber)
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 120
    }

You can use prepare Segue to access Container controller and save its reference

class Master: UIViewController, ContainerToMaster {

@IBOutlet var containerView: UIView!

var containerViewController: Container?

@IBOutlet var labelMaster: UILabel!

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "containerViewSegue" {
        containerViewController = segue.destinationViewController as? Container
        
    }
}

And then you can use containerViewController to pass data

containerViewController?.arryOfOrder  = ArrayOfOrder

or you can have function in container... pass ArrayOfOrder to that function.. which will also reload tableView

containerViewController?.updateData(dataArray: ArrayOfOrder)

If you want to pass data from parent to child on an action that happened in the parent class, you can access the children of the current UIViewController using

children.forEach { (viewController) in
     if let temp = viewController as? InstantVC{
          temp.property = dataToPass
     }
}

The above will only work if both your view controllers are kept as children to the parent at all times and are never removed. If you remove them as children and re-add them at a later stage you will need to maintain references to the child and update that reference since the children array wont have that UIViewController . But since you are using storyboards, this should work fine.

If you want to access data from on action on the children class, you can access the parent view controller using:

let dataToUse = (parent as? OfflineVC).someProperty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM