[英]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.