我正在Clean架构中开发am应用程序,我想在用户登录后将数据传递到主屏幕,这是我的路由器文件:
import UIKit
protocol LoginRoutingLogic: class {
func routeToRegisterController()
func routeToRecoveryPassword()
func routeToMainPage()
}
protocol LoginDataPassing: class {
var dataStore: LoginDataStore? { get }
}
final class LoginRouter: LoginRoutingLogic, LoginDataPassing {
weak var viewController: LoginController?
var dataStore: LoginDataStore?
func routeToRegisterController() {
let storyboard = UIStoryboard(name: Constants.Identifiers.registerControllerIdentifier, bundle: nil)
if let viewcontroller = storyboard.instantiateViewController(withIdentifier: Constants.Identifiers.registerControllerIdentifier) as? RegisterController {
viewController?.navigationController?.pushViewController(viewcontroller, animated: true)
}
}
func routeToRecoveryPassword() {
let storyboard = UIStoryboard(name: Constants.Identifiers.forgotPasswordControllerIdentifier, bundle: nil)
if let viewcontroller = storyboard.instantiateViewController(withIdentifier: Constants.Identifiers.forgotPasswordControllerIdentifier) as? RecoveryPasswordController {
viewController?.present(viewcontroller, animated: true, completion: nil)
}
}
func routeToMainPage() {
let storyboard = UIStoryboard(name: "MainPageController", bundle: nil)
if let viewcontroller = storyboard.instantiateViewController(withIdentifier: "MainPageController") as? MainPageController {
viewController?.navigationController?.pushViewController(viewcontroller, animated: true)
}
}
}
我的用户模型,我想将值传递到主页,使调用正常工作:
import Foundation
struct User: Codable {
var token: String?
var name: String?
var email: String?
var password: String?
var statusCode: Int?
}
struct LoginError: Codable {
}
extension User {
static func parse(responseData: Data?) -> User? {
var user: User?
guard let data = responseData else {
return user
}
do {
let decoder = JSONDecoder()
user = try decoder.decode(User.self, from: data)
} catch let err {
print("Error: ", err)
}
return user
}
}
以及我想将用户名传递到用户名标签的viewController:
import UIKit
protocol MainPageDisplayLogic: class {
func getData(viewModel: LoginModel.Fetch.ViewModel)
}
final class MainPageController: UIViewController {
var interactor: MainPageBusinessLogic?
var router: (MainPageRoutingLogic & MainPageDataPassing)?
var builder = MainPageBuilder()
// MARK: Object lifecycle
override func viewDidLoad() {
super.viewDidLoad()
setUpView()
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
// MARK: Setup
private func setup() {
let viewController = self
let interactor = MainPageInteractor()
let presenter = MainPagePresenter()
let worker = MainPageWorker()
let router = MainPageRouter()
viewController.interactor = interactor
viewController.router = router
interactor.presenter = presenter
interactor.worker = worker
presenter.viewController = viewController
router.viewController = viewController
router.dataStore = interactor
}
}
extension MainPageController: MainPageDisplayLogic {
func getData(viewModel: LoginModel.Fetch.ViewModel) {
let name = viewModel.name
builder.usernameLabel.text = name
}
}
extension MainPageController: ViewCodeProtocol {
func setUpView() {
viewHierarchy()
makeConstraits()
setupViewNavigationBar()
}
func viewHierarchy() {
view.addSubview(builder.usernameLabel)
}
func makeConstraits() {
builder.usernameLabelConstraits()
}
func setupViewNavigationBar() {
navigationController?.setNavigationBarHidden(false, animated: true)
}
}
主页上的路由器,我还不能正确地计算数据存储的事情,如何在干净的swift中在控制器之间传递数据?至少没有用户默认值。
import Foundation
protocol MainPageRoutingLogic: class {
}
protocol MainPageDataPassing: class {
var dataStore: MainPageDataStore? { get }
}
final class MainPageRouter: MainPageRoutingLogic, MainPageDataPassing {
weak var viewController: MainPageController?
var dataStore: MainPageDataStore?
}
我已经明白了,我只需要使用目标ViewController中的dataStore引用并检索数据。