【问题标题】:Clean swift how to pass data back to viewController快速清理如何将数据传递回viewController
【发布时间】:2018-02-06 09:08:10
【问题描述】:
class ListBillPaymentFavoriteRouter: NSObject, ListBillPaymentFavoriteRoutingLogic, ListBillPaymentFavoriteDataPassing {
    weak var viewController: ListBillPaymentFavoriteViewController?
    var dataStore: ListBillPaymentFavoriteDataStore?

    // MARK: Routing

    func routeToBillPaymentInput() {
        let destinationVC = BillPaymentInputViewController.instantiate()
        var destinationDS =  destinationVC.router!.dataStore!
        passDataToBillPaymentInput(source: dataStore!, destination: &destinationDS)
        navigationToBillPaymentInput(source: viewController!, destination: destinationVC)
    }

    // MARK: Navigation

    func navigationToBillPaymentInput(source: ListBillPaymentFavoriteViewController, destination: BillPaymentInputViewController) {
        source.navigationController?.pop_FromLeftMoveToRight()
    }

    // MARK: Passing data

    func passDataToBillPaymentInput(source: ListBillPaymentFavoriteDataStore, destination: inout BillPaymentInputDataStore) {
        destination.testTest = "Yessssss"
    }


}

当我弹出到导航控制器时,我无法接收数据

【问题讨论】:

  • 使用delegationNotification
  • 如果我不想使用委托,我应该这样做吗?
  • @PuChaisirikul 这是 iOS 使用委托的常用方式。但是你可以使用回调来代替它们
  • 使用 swift Closure 传回数据

标签: swift swift3


【解决方案1】:

我们应该可以viewController,例子

func routeToBillPaymentInput() {
    let index = viewController!.navigationController!.viewControllers.count - 2
    let destinationVC = viewController?.navigationController?.viewControllers[index] as! BillPaymentInputViewController
    var destinationDS =  destinationVC.router!.dataStore!
    passDataToBillPaymentInput(source: dataStore!, destination: &destinationDS)
    navigationToBillPaymentInput(source: viewController!, destination: destinationVC)
}

【讨论】:

    【解决方案2】:

    完全

    class ListBillPaymentFavoriteRouter: NSObject, ListBillPaymentFavoriteRoutingLogic, ListBillPaymentFavoriteDataPassing {
    weak var viewController: ListBillPaymentFavoriteViewController?
    var dataStore: ListBillPaymentFavoriteDataStore?
    
    // MARK: Routing
    
    func routeToBillPaymentInput() {
        let index = viewController!.navigationController!.viewControllers.count - 2
        let destinationVC = viewController?.navigationController?.viewControllers[index] as! BillPaymentInputViewController
        var destinationDS =  destinationVC.router!.dataStore!
        passDataToBillPaymentInput(source: dataStore!, destination: &destinationDS)
        navigationToBillPaymentInput(source: viewController!, destination: destinationVC)
    }
    
    // MARK: Navigation
    
    func navigationToBillPaymentInput(source: ListBillPaymentFavoriteViewController, destination: BillPaymentInputViewController) {
        source.navigationController?.popViewController(animated: true)
    }
    
    // MARK: Passing data
    
    func passDataToBillPaymentInput(source: ListBillPaymentFavoriteDataStore, destination: inout BillPaymentInputDataStore) {
        destination.testTest = "Yessssss"
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      相关资源
      最近更新 更多