简体   繁体   中英

RxSwift doesn't push back data from one ViewController to another

I am trying to push back data from SecondViewController to FirstViewController. I am trying to do this using RxSwift's PublishSubject type. When I move to FirstViewController I get only information about what I pushed in the console. I just want to get this data in testArray object, that will be shown in UITableView. There is no error showing data in UITableView, I checked. Any ideas? ;)

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)
    }
}

I figured out why It wasn't working. In SecondViewController in didSelectRowAt method I used pushViewController to move to FirstViewController - I delete that. Then I enabled to back through back button: navigationItem.setHidesBackButton(false, animated: true)

But now my question is why it wasn't working when I was using pushViewController?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM