我有两个视图控制器: 1. 结帐VC 2. 交货时间VC
在 DeliveryTimeVC 中,我有以下变量:
class DeliveryTimeVC: UIViewController {
var tableViewDay:String = ""
}
我使用以下推送从CheckoutVC
转到DeliveryTimeVC
:
let storyboard = UIStoryboard(name: Storyboard.DeliveryTimeStoryboard, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: StoryboardId.DeliveryTimeVC)
navigationController?.pushViewController(controller, animated: true)
DeliveryTimeVC
我有一个表视图,在 didSelect 上我回到CheckoutVC
didSelect
在 func 中,我有以下代码在离开控制器之前附加我的变量:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableViewDay.removeAll()
tableViewDay.append(cell.weekDayLbl.text!)
navigationController?.popViewController(animated: true)
}
如何将字符串变量数据从DeliveryTimeVC
传输到CheckoutVC
中的另一个变量?假设我在 CheckoutVC 中创建一个变量,如下所示:
class CheckoutVC: UIViewController, CartProductCellDelegate {
var tableViewDayTransferedData:String = ""
}
如何将数据从tableViewDay:String
传输到tableViewDayTransferedData:String
好吧,您可以使用此行
let storyboard = UIStoryboard(name: "NAMEOFYOURSTORYBOARD", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "IDOFYOURVIEW") as! CheckoutVC
vc.tableViewDayTransferedData = self. tableViewDay
self.navigationController!.pushViewController(vc, animated: true)
根据上面的@Hassan Shahbazi 评论,我能够稍微更改他的代码并获得适合我的最终答案:
protocol DeliveryTimeDelegate: class {
func didGetData(tableViewDayTransferedData: String)
}
class DeliveryTimeVC: UIViewController {
weak var delegate: DeliveryTimeDelegate?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableViewDay.removeAll()
tableViewDay.append(cell.weekDayLbl.text!)
delegate?.didGetData(tableViewDayTransferedData: tableViewDay)
navigationController?.popViewController(animated: true)
}
}
class CheckoutVC: UIViewController, DeliveryTimeDelegate {
func ...() {
let storyboard = UIStoryboard(name: Storyboard.DeliveryTimeStoryboard, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: StoryboardId.DeliveryTimeVC) as! DeliveryTimeVC
controller.delegate = self
navigationController?.pushViewController(controller, animated: true)
}
func didGetData(tableViewDayTransferedData: String) {
tableViewDayTransferedData = tableViewDayTransferedData
}
}
在最后一个函数中,您会看到两个相同的变量;它们只是具有相同的名称:tableViewDayTransferedData = tableViewDayTransferedData
第一个来自CheckoutVC。 第二个来自协议。
iOS 中的数据/变量传递通常有 2 种主要模式。
委托模式
Objective-c时代的流行模式,但仍然有用且易于操作。您需要定义一个delegate protocol
并将其用于传递变量。
protocol DeliveryTimeDelegate: class {
func didGetData(tableViewDay: String)
}
class DeliveryTimeVC: UIViewController {
weak var delegate: DeliveryTimeDelegate?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableViewDay.removeAll()
tableViewDay.append(cell.weekDayLbl.text!)
delegate?.didGetData(tableViewDay: "YOUR_VALUE")
navigationController?.popViewController(animated: true)
}
}
class CheckoutVC: UIViewController, CartProductCellDelegate {
func ...() {
let storyboard = UIStoryboard(name: Storyboard.DeliveryTimeStoryboard, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: StoryboardId.DeliveryTimeVC)
controller.delegate = self
navigationController?.pushViewController(controller, animated: true)
}
}
extension CheckoutVC: DeliveryTimeDelegate {
func didGetData(tableViewDay: String) {
tableViewDayTransferedData = tableViewDay
}
}
闭合模式
该模式可以看作是delegate
的快速友好实现。在使用closure
时,请务必充分注意内存管理并保留周期问题。
class DeliveryTimeVC: UIViewController {
var onDataTransfered: ((String) -> Void)?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableViewDay.removeAll()
tableViewDay.append(cell.weekDayLbl.text!)
self.onDataTransfered?("YOUR_VALUE")
navigationController?.popViewController(animated: true)
}
}
class CheckoutVC: UIViewController, DeliveryTimeDelegate {
func ...() {
let storyboard = UIStoryboard(name: Storyboard.DeliveryTimeStoryboard, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: StoryboardId.DeliveryTimeVC)
controller.onDataTransfered = { [weak self] tableViewDayTransferedData in
self?.tableViewDay = tableViewDayTransferedData
}
navigationController?.pushViewController(controller, animated: true)
}
}