簡體   English   中英

API 調用成功完成 SWIFT 后不顯示警報視圖控制器

[英]Alert view controller not presented once API call successfully completed SWIFT

我有以下 API 調用:

    func updateSheetVals() {
        let args = ["id": viewModel.id]

        let measurements = UpdateRequest.Measurements(
             departure: Double(sheet.postRefuelQuantity.field.currentValue),
             discrepancy: Double(sheet.discrepancy.field.currentValue),
             discrepancyPercentage: Double(sheet.discrepancyPercent.field.currentValue),
             preTotal: Double(dheet.preTotal.field.currentValue),
             targetTotal: Double(fuelSheet.requiredTotal.field.currentValue)
        )

        let body = UpdateRequest(measurement: measurements)

        API.client.post(.updateFuelSheetVals, with: args, using: .put, posting: body, expecting: MessageResponse.self) { (success, response) in
            switch success {
            case .failure:
                print("Check network connection")
            case .success:
                DispatchQueue.asyncMain {
                    self.present(WarningViewController.finished(), animated: true)
                }
            }
        }
    }
}

然而,即使我收到 200 響應並且正確調用了 API,我的視圖控制器也永遠不會顯示。 如果需要,很高興提供更多上下文代碼,但首先想知道我是否只是錯過了這個塊的明顯內容......

編輯:

API 調用在以下代碼中觸發:

   func acceptButtonPressed(_ button: Button) {
        var confirmation: UIViewController & ConfirmationAction

        guard let level = viewModel.getSelectedSheet().order.levelDouble else { return }

        if self.viewModel.requiresSignature {

            if level < 3 {
                confirmation = SignatureViewController(hasDiscrepancy: viewModel.hasDiscrepancy, discrepancyPrompt: viewModel.discrepancyPrompt, sl: level)
            } else {
                confirmation = SignatureViewController(hasDiscrepancy: viewModel.hasDiscrepancy,
                                                            discrepancyPrompt: viewModel.discrepancyPrompt, sl: level)
            }

        } else {
            if let userInputAllowed = sheet.userInputAllowed, level < 3, !userInputAllowed {
                confirmation = OrderAcceptAlertViewController.alert()
            } else if level < 3 {
                confirmation = DiscrepancyAlertViewController.alertWithDiscrepancy(hasDiscrepancy: viewModel.hasDiscrepancy,
                                                                                   discrepancyPrompt: viewModel.discrepancyFromManualInput(discrepancyValue: fuelSheet.percentageDiscrepancy.field.currentValue))
            } else {
                confirmation = DiscrepancyAlertViewController.alertWithDiscrepancy(hasDiscrepancy: viewModel.hasDiscrepancy,
                                                                                   discrepancyPrompt: viewModel.discrepancyPrompt)
            }
        }

        confirmation.confirmationAction = { [weak confirmation, weak self] response in
            guard let self = self else {
                return
            }

            var completedSignature: SignatureParameter?

            switch response {
            case let .signature(signature):
                completedSignature = signature
            case .discrepancy:
                break
            }

            let args = ["id": self.viewModel.id]
            let params = AcceptParameter(
                employee: self.viewModel.employee,
                signature: completedSignature,
                base64FuelSheet: self.sheet.ticket?.base64
            )

            if let confirm = confirmation {
                confirm.setLoading(true)

                API.client.post(.accept, with: args, using: .put, posting: params, expecting: Nothing.self, completion: { [weak self] (success, _) in
                    DispatchQueue.asyncMain {
                        guard let self = self else {
                            return
                        }

                        confirm.setLoading(false)
                            self.navigationController?.popViewController(animated: true) 
                        }
                    }
                })
                self.updateSheetVals()
            }
        }
        present(confirmation, animated: true, completion: nil)
    }

這肯定會起作用:

case .success:
      DispatchQueue.asyncMain {
            let viewController = self.storyboard?.instantiateViewController(withIdentifier: WarningViewController) as! WarningViewController
            self.present(viewController, animated: true, completion: nil)
      }
}

為了這

self.navigationController?.popViewController(animated: true)  

要工作 vc 必須在導航控制器內,如果不是這個self.navigationController? 將是nil並且什么也不會顯示,你要么需要一個 segue/push.present 像

self.present(viewController, animated: true, completion: nil) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM