将字符串变量数据从一个 Class/ViewController 传递到 Class/ViewController

Pass String Variable Data from one Class/ViewController to Class/ViewController

我有两个视图控制器: 1.结帐VC 2. DeliveryTimeVC

在 DeliveryTimeVC 中我有以下变量:

class DeliveryTimeVC: UIViewController {
var tableViewDay:String = ""
}

我使用以下推送从 CheckoutVCDeliveryTimeVC

let storyboard = UIStoryboard(name: Storyboard.DeliveryTimeStoryboard, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: StoryboardId.DeliveryTimeVC)
navigationController?.pushViewController(controller, animated: true)

DeliveryTimeVC 我有一个 tableView,在 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)

data/variable传入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
    }
}

闭包模式

该模式可以看作 swift 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)
    }
}

根据@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。第二个来自协议。