0

这是我的 FirstView(父视图)

tableView.rx.modelSelected(Kinder.self)
        .asDriver()
        .drive(self.detailKinderViewModel.currentKinder)
        .disposed(by: disposeBag)

这是 ViewModel ( BehaviorRelay )

    lazy var currentKinder = BehaviorRelay<Kinder>(value: Kinder())

这是我的 SecondView(子视图)

override func viewDidLoad() {
    super.viewDidLoad()
    detailKinderViewModel.currentKinder
        .asDriver(onErrorJustReturn:Kinder())
        .map{$0.kinder_name}
        .drive(self.navigationItem.rx.title)
        .disposed(by: disposeBag)
}

我只能获得默认模型数据。

我想获取有关子视图的当前数据

4

1 回答 1

0

没有足够的代码来找出您的问题可能是什么。以下是我使用我的Cause_Logic_Effect 库的方法。

import Cause_Logic_Effect
import RxCocoa
import RxSwift
import UIKit

extension FirstView {
    func connect() {
        Observable.just([
            Kinder(name: "Ben"),
            Kinder(name: "Mia"),
            Kinder(name: "Leon"),
            Kinder(name: "Emma")
        ])
            .bind(to: tableView.rx.items(cellIdentifier: "Cell")) { _, kinder, cell in
                if #available(iOS 14.0, *) {
                    var configuration = cell.defaultContentConfiguration()
                    configuration.text = kinder.name
                    cell.contentConfiguration = configuration
                }
                else {
                    cell.textLabel?.text = kinder.name
                }
            }
            .disposed(by: disposeBag)
        
        // when the user selects a cell, load its Kinder into a newly created
        // SecondView and push it onto the navigation stack
        tableView.rx.modelSelected(Kinder.self)
            .bind(onNext: pushScene(on: navigationController!, animated: true) { kinder in
                SecondView().scene { $0.connect(kinder: kinder) }
            })
            .disposed(by: disposeBag)
    }
}

extension SecondView {
    func connect(kinder: Kinder) -> Observable<Never> {
        // use the Kinder
        title = kinder.name
        
        // this controller doesn't send any info back to its parent.
        return .never()
    }
}

// The view controllers.
final class FirstView: UIViewController {
    var tableView: UITableView!
    let disposeBag = DisposeBag()
    
    override func loadView() {
        super.loadView()
        title = "Main"
        tableView = UITableView(frame: view.bounds)
        tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        view.addSubview(tableView)
    }
}

final class SecondView: UIViewController {
    override func loadView() {
        super.loadView()
        view.backgroundColor = .white
    }
}

// The models
struct Kinder {
    let name: String
}

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        window = UIWindow(frame: UIScreen.main.bounds)
        // Create a FirstView, configure it with a connector, wrap it in a
        // navigation controller and make that the root.
        window?.rootViewController = UINavigationController(rootViewController: FirstView().configure { $0.connect() })
        window?.makeKeyAndVisible()

        return true
    }
}
于 2021-10-30T18:22:07.233 回答