我有 2 个具有相同逻辑的视图控制器,但其中一个不工作

I have 2 view controllers with same logic but one of them is not working

我有 2 个视图模型 TransferViewModel,它有各自的 TransferViewController,用于从模型 LocalTransactionRequest 进行本地交易,我有 BankTransferViewModel,它有模型 BankTransactionsRequest,第一个正在工作,但第二个没有,两个视图控制器都是应该对另一个视图控制器 ConfirmViewController 执行 segue,但是第二个(BankTransferViewController)不工作

[这个是TransferViewController][1]

    private func setupViewModel() {
    viewModel.isTransfer = isTransfer
    viewModel.loan = loan
    viewModel.getBalance()
    
    
    transferButton.rx.tap.asObservable().subscribe(onNext: { [weak self] _ in
        guard let strongSelf = self else { return }
        if let isVerified = UserManager.shared.get()?.IsVerified.value, isVerified{
            strongSelf.viewModel.phoneNumberText.accept(strongSelf.phoneNumberTextField.text ?? "")
            strongSelf.viewModel.amountText.accept(strongSelf.amountTextField.text ?? "")
            strongSelf.viewModel.transfer()
        }else{
            strongSelf.showVerificationAlert()
        }
    }).disposed(by: disposeBag)
    
    viewModel.accountInfo.asObservable().subscribe(onNext: { [weak self] accountInfo in
        if let account = accountInfo{
            guard let strongSelf = self else { return }
            strongSelf.accountInfo = account
            let request = LocalTransactionRequest(Identification: UserManager.shared.identification ?? "", Amount: Double(strongSelf.amountTextField.text!)!, ReceiverPhoneNumber: strongSelf.phoneNumberTextField.text!, IDBankAccount: UserManager.shared.defaultBankAccountId ?? -1, IsFromTransfer: strongSelf.isTransfer, Description: strongSelf.descriptionTF.text!)
            strongSelf.transferRequest.accept(request)
            strongSelf.performSegue(withIdentifier: "segue_toConfirmTransfer", sender: account)
        }
    }).disposed(by: disposeBag)
}

[这个是 BankTransferViewController][2]

    private func setupViewModel(){
    viewModel.isTransfer = isTransfer
    viewModel.getBalance()

    transferButton.rx.tap.asObservable().subscribe(onNext: { [weak self] _ in
        guard let strongSelf = self else { return }
        if let isVerified = UserManager.shared.get()?.IsVerified.value, isVerified{
            strongSelf.viewModel.bankNumberText.accept(strongSelf.bankNumberTextField.text ?? "")
            strongSelf.viewModel.firstName.accept(strongSelf.firstNameTextField.text ?? "")
            strongSelf.viewModel.lastName.accept(strongSelf.lastNameTextField.text ?? "")
            strongSelf.viewModel.amountText.accept(strongSelf.amountTextField.text ?? "")
            strongSelf.viewModel.descriptionText.accept(strongSelf.descriptionTF.text ?? "")
            strongSelf.viewModel.transferNational()
        }else{
            strongSelf.showVerificationAlert()
        }
    

    }).disposed(by: disposeBag)
    
    viewModel.transferRequest.asObservable().subscribe(onNext: { [weak self] bankRequest in
        if let bank = bankRequest{
            guard let strongSelf = self else { return }
            strongSelf.bankTransferRequest = bank
            let request = BankTransactionRequest(Identification: UserManager.shared.identification ?? "", ReceiverBankAccount: strongSelf.bankNumberTextField.text!, ReceiverFirst: strongSelf.firstNameTextField.text!, ReceiverLast: strongSelf.lastNameTextField.text!, Amount: Double(strongSelf.amountTextField.text!)!, Description: strongSelf.descriptionTF.text!)
            strongSelf.nationalTransferRequest.accept(request)
            DispatchQueue.main.async {
                strongSelf.performSegue(withIdentifier: "segue_toConfirmTransfer", sender: bank)
            }
        }
    }).disposed(by: disposeBag)
    }


This is view model of BankTransferViewController

import RxCocoa
import RxSwift

class BankTransferViewModel: BaseViewModel {
    private let transferUseCase: TransferUseCase
    
    var accountInfo: BehaviorRelay<AccountExistModel?> = BehaviorRelay(value: nil)
    var balance: BehaviorRelay<BalanceModel?> = BehaviorRelay(value: nil)
    var bankNumberText: BehaviorRelay<String> = BehaviorRelay(value: "")
    var firstName: BehaviorRelay<String> = BehaviorRelay(value: "")
    var lastName: BehaviorRelay<String> = BehaviorRelay(value: "")
    var amountText: BehaviorRelay<String> = BehaviorRelay(value: "")
    var descriptionText: BehaviorRelay<String> = BehaviorRelay(value: "")
    var transferRequest: BehaviorRelay<BankTransactionRequest?> = BehaviorRelay(value: nil)

    var accountExist = PublishSubject<Bool>()
    var hasMoney = PublishSubject<Bool>()
    var invalidBankNumber = PublishSubject<Bool>()
    var accountCannotRecieve = PublishSubject<Bool>()
    var isTransfer : Bool = true
    
    var transferPressed: AnyObserver<Void> {
        return AnyObserver { [weak self] event in
            switch event {
            case .next:
                guard let strongSelf = self else {
                    return
                }
                strongSelf.checkValidation()
            default:
                break
            }
        }
    }
    
    init(transferUseCase: TransferUseCase) {
        self.transferUseCase = transferUseCase
    }
    
    func transferNational() {
        self.checkValidation()
    }
    
    private func checkValidation() {
        guard let balance = self.balance.value else {
            state.onNext(.error(error: RepoError(with: "Dështoi verifikimi i disponueshmërisë financiare. Ju lusim të provoni më vonë.")))
            return
        }
        
        
        if bankNumberText.value == ""{
            state.onNext(.error(error: RepoError(with: "Plotëso fushën për numrin e bankës të pranuesit.")))
            return
        }
        
        if bankNumberText.value.count < 6{
            state.onNext(.error(error: RepoError(with:"Ju lutemi, shtypni një numër valid të gjirollogarisë")))
            return
        }
        
        guard let doubleAmount = Double(amountText.value), doubleAmount > 0 else {
            state.onNext(.error(error: RepoError(with: "Shuma jo e rregullt")))
            return
        }
        
        if amountText.value == ""{
            state.onNext(.error(error: RepoError(with: "Shuma jo e 
    saktë.")))
            return
        }
        
        
        if balance.Balance < doubleAmount{
            state.onNext(.error(error: RepoError(with: "Nuk keni fonde të mjaftueshme për realizimin e transaksionit.")))
            return
        }
    
        
        if bankNumberText.value != "" && amountText.value != "" && (balance.Balance >= doubleAmount) {
            
//            checkAccountExist()
        }
    }
    
    
        func checkModulus16(accountNumber: String) -> Bool {
        if accountNumber.isEmpty{
            return false
        }
        
        let newValue = accountNumber.dropLast(2)
        let mod = Int64(newValue + "00")! % 97
        let result = 98 - mod
        let derivedData = newValue + "" + (result < 10 ? "0\(result)" : "\(result)")
        
        return Int64(derivedData) == Int64(accountNumber)
    }
    
    
        func showError(with message: String ) {
          state.onNext(.error(error: RepoError(with: message)))
    }
    
    
        func getBalance(){
        let params = ["Identification": UserManager.shared.identification ?? "" ] as ApiJson
        transferUseCase.getBalance(with: params) {[weak self] (balance, error) in
            guard let strongSelf = self else { return }
            if let error = error {
                strongSelf.state.onNext(.error(error: error))
                strongSelf.accountExist.onNext(false)
            }else if let balance = balance{
                UserManager.shared.userBonus = balance.BonusAmount
                strongSelf.state.onNext(.content)
                strongSelf.balance.accept(balance)
                strongSelf.accountExist.onNext(true)
                UserManager.shared.updateBalance(with: balance)
            }
        }
    }
    
    //MARK: - baseViewModel
    
        override func tryAgain() {
        self.getBalance()
        
    }

}

确保以下几点对您的 performSegue 在 BankTransferViewController 中工作有效:

  • BankTransferViewController 有一个指向 ConfirmViewController 的 segue。
  • performSegue(withIdentifier: yourIdentifier, sender: yourModel) 中的标识符与故事板中连接两个视图控制器的 segue 完全相同。
  • 由于您是在 viewModel.transferRequest.asObservable().subscribe(onNext: 代码中使用它,因此请确保您在代码中的某处向 viewModel.transferRequest 发出了一个值。否则,performSegue 永远不会被调用。
  • 由于您在使用 performSegue 之前进行了此检查 if let bank = bankRequest{,请确保您发出的 transferRequest 值不为零。