2016-07-18 3 views
7

Как я могу отправить данные из модуля A в модуль B в VIPER? Я использую маршрутизатор A, который имеет информацию для модуля B, и попытайтесь отправить эту информацию для просмотра контроллера B или ведущего B. Каков наилучший способ сделать это?Не удается отправить данные на другой модуль в VIPER

+0

вы читали "Книгу VIPER" по Рамблере? – Alexander

ответ

4

В этом случае мой рабочий процесс:

  1. Обычно пользовательский интерфейс (view) в модуле A запускает событие, которое запускает модуль B.
  2. событие достигает в модуле А. presenterpresenter знает, что он должен изменить модуль и уведомляет wireframe, который знает, как внести изменения.
  3. wireframe в модуле А уведомляет к wireframe в модуле B. В этом вызове передает все данные, необходимые
  4. wireframe в модуле B продолжает свою нормальную работу, передавая информацию в presenter

wireframe в модуле A должно быть известно wireframe B

1

wireframe имеют ссылку на презентатора? This version of VIPER which i use

маршрутизатор знает о другом модуле и говорит вид, чтобы открыть его. Монтаж объединяет все части модуля.

2

Используйте делегатов для передачи данных между модулями VIPER:

// 1. Declare which messages can be sent to the delegate 
 

 
// ProductScreenDelegate.swift 
 
protocol ProductScreenDelegate { 
 
//Add arguments if you need to send some information 
 
    func onProductScreenDismissed() 
 
    func onProductSelected(_ product: Product?) 
 
} 
 

 
// 2. Call the delegate when you need to send him a message 
 

 
// ProductPresenter.swift 
 
class ProductPresenter { 
 

 
    // MARK: Properties 
 
    weak var view: ProductView? 
 
    var router: ProductWireframe? 
 
    var interactor: ProductUseCase? 
 
    var delegate: ProductScreenDelegate? 
 
} 
 

 
extension ProductPresenter: ProductPresentation { 
 

 
    //View tells Presenter that view disappeared 
 
    func onViewDidDisappear() { 
 

 
     //Presenter tells its delegate that the screen was dismissed 
 
     delegate?.onProductScreenDismissed() 
 
    } 
 
} 
 

 
// 3. Implement the delegate protocol to do something when you receive the message 
 

 
// ScannerPresenter.swift 
 
class ScannerPresenter: ProductScreenDelegate { 
 

 
    //Presenter receives the message from the sender 
 
    func onProductScreenDismissed() { 
 

 
     //Presenter tells view what to do once product screen was dismissed 
 
     view?.startScanning() 
 
    } 
 
    ... 
 
} 
 

 
// 4. Link the delegate from the Product presenter in order to proper initialize it 
 

 
// File ScannerRouter.swift 
 
class ProductRouter { 
 

 
    static func setupModule(delegate: ProductScreenDelegate?) -> ProductViewController { 
 
     ... 
 
     let presenter = ScannerPresenter() 
 

 
     presenter.view = view 
 
     presenter.interactor = interactor 
 
     presenter.router = router 
 
     presenter.delegate = delegate // Add this line to link the delegate 
 
     ... 
 
     } 
 
}

Дополнительные советы, проверить этот пост https://www.ckl.io/blog/best-practices-viper-architecture/