【问题标题】:RxSwift doesn't push back data from one ViewController to anotherRxSwift 不会将数据从一个 ViewController 推回另一个
【发布时间】:2020-08-14 13:13:03
【问题描述】:

我正在尝试将数据从 SecondViewController 推回 FirstViewController。 我正在尝试使用 RxSwift 的 PublishSubject 类型来做到这一点。 当我移动到 FirstViewController 时,我只获得有关我在控制台中推送的内容的信息。 我只想在 testArray 对象中获取这些数据,这些数据将显示在 UITableView 中。 在 UITableView 中显示数据没有错误,我检查了。 有任何想法吗? ;)

FirstViewController.swift

let disposeBag = DisposeBag()

@objc func addCryptoButtonPressed() {
    DispatchQueue.main.async {
        let viewController = SecondViewController()
        viewController.subject
            .subscribe(onNext: { [weak self] pushedData in
                print(pushedData)                         // <----- it is working
                self?.testArray.append(pushedData)        // <----- it is not
            }).disposed(by: self.disposeBag)
        DispatchQueue.main.async {
            self.mainTableView.reloadData()
        }
        self.navigationController?.pushViewController(viewController, animated: true)
    }
}

SecondViewController.swift

let subject = PublishSubject<String>()

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    DispatchQueue.main.async {
        let name = self.names[indexPath.row]
        self.subject.onNext(name)
        let viewController = FirstViewController()
        self.navigationController?.pushViewController(viewController, animated: true)
    }
}

【问题讨论】:

    标签: ios uitableview uiviewcontroller rx-swift


    【解决方案1】:

    我知道为什么它不起作用。 在 didSelectRowAt 方法的 SecondViewController 中,我使用 pushViewController 移动到 FirstViewController - 我删除了它。 然后我启用了通过后退按钮返回: navigationItem.setHidesBackButton(false, animated: true)

    但现在我的问题是为什么我使用 pushViewController 时它不起作用?

    【讨论】:

    • 因为你所做的创建了一个FirstViewController 的新实例,然后将其推送到SecondViewController 之上的堆栈中。如果您使用可视化调试器对其进行检查,您应该会看到一个包含三个 VC 的堆栈。由于这是与第一个完全不同的对象,因此它没有更新工作所需的订阅。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多