首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >模型视图上的RxSwift modelSelected驱动模型&在DetailView上获取该模型

模型视图上的RxSwift modelSelected驱动模型&在DetailView上获取该模型
EN

Stack Overflow用户
提问于 2021-10-30 08:04:19
回答 1查看 38关注 0票数 0

这是我的FirstView (父级VIew)

代码语言:javascript
代码运行次数:0
运行
复制
tableView.rx.modelSelected(Kinder.self)
        .asDriver()
        .drive(self.detailKinderViewModel.currentKinder)
        .disposed(by: disposeBag)

这是ViewModel ( BehaviorRelay )

代码语言:javascript
代码运行次数:0
运行
复制
    lazy var currentKinder = BehaviorRelay<Kinder>(value: Kinder())

这是我的SecondView (子视图)

代码语言:javascript
代码运行次数:0
运行
复制
override func viewDidLoad() {
    super.viewDidLoad()
    detailKinderViewModel.currentKinder
        .asDriver(onErrorJustReturn:Kinder())
        .map{$0.kinder_name}
        .drive(self.navigationItem.rx.title)
        .disposed(by: disposeBag)
}

我只能得到默认的模型数据。

我想获取子视图上的最新数据

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-30 18:22:07

没有足够的代码来找出您的问题所在。下面是我如何使用我的Cause_Logic_Effect library来做这件事。

代码语言:javascript
代码运行次数:0
运行
复制
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
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69777824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档