简体   繁体   English

如何将数据从视图 Controller 传递到 swift 中的容器视图

[英]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.我想将数据从离线视图 Controller 传递到 InstantVC。 I don't know how to do that.我不知道该怎么做。 Basically, I have segmented Controller.基本上,我已经分段了 Controller。 When user tab Instant it show the segmented view controller and hide the Schedule view controller.当用户选项卡 Instant 它显示分段视图 controller 并隐藏计划视图 controller。 And pass data according to selected segmented.并根据选定的分段传递数据。

Here is the Offline View controller to pass data这里是离线查看 controller 来传递数据


                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这是来自离线 VC 的不稳定数据的即时视图 controller

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您可以使用 prepare Segue 访问 Container controller 并保存其引用

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来传递数据

containerViewController?.arryOfOrder  = ArrayOfOrder

or you can have function in container... pass ArrayOfOrder to that function.. which will also reload tableView或者您可以在容器中使用 function... 将 ArrayOfOrder 传递给该 function.. 这也将重新加载 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如果您想将父级 class 中发生的操作的数据从父级传递给子级,您可以使用访问当前UIViewController的子级

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 .如果您将它们作为子级删除并在稍后阶段重新添加它们,您将需要维护对子级的引用并更新该引用,因为子级数组不会具有该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:如果您想从子 class 上的操作访问数据,您可以使用以下方法访问父视图 controller:

let dataToUse = (parent as? OfflineVC).someProperty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从View控制器传递到Container? - How to pass data from View controller to Container? 如何将数据从View Controller传递到Container View? - How would I pass data from a View Controller to a Container View? 如何在Swift中将数据从父视图控制器多次传递到Container VC Child? - How to Pass Data Multiple Times from Parent View Controller to Container VC Child in Swift? 如何在iOS Swift中使用回调在视图控制器和容器视图之间传递数据? - How to pass data between view controller and Container view using callbacks in iOS Swift? 如何将数据从一个视图控制器传递到另一个SWIFT - How to pass data from one view controller to the other SWIFT 如何将数据从子视图传递给父视图控制器? 在迅速 - How to pass data from child to parent view controller? in swift 如何使用Swift将通知数据传递给View Controller - How to pass notification data to a View Controller with Swift Swift,如何将 tableview 数据传递给视图控制器? - Swift, how to pass tableview data to view controller? 在容器视图中将数据传递到控制器 - Pass data to controller in container view 将数据传递给Swift中的Container View? - Pass data to the Container View in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM