尝试在其视图不在窗口层次结构中的...上呈现...

时间:2019-01-23 20:00:51

标签: ios swift segue

当付款已正确处理后,我正在尝试对“成功窗口”执行查询。我正在尝试通过使用

来做到这一点
self.performSegue(withIdentifier: "successView", sender: self)

在我的addCardViewController函数内部。 (显示在这里:)

    func addCardViewController(_ addCardViewController: STPAddCardViewController, didCreateToken token: STPToken, completion: @escaping STPErrorBlock) {

    // Monetary amounts on stripe are based on the lowest monetary unit (i.e. cents),
    // therefore, we need to multiply the dollar amount by 100 to get the correct amount.
    let stripeAmount = toPay * 100

    // Call the 'stripeCharge' Firebase cloud function, with user's card token and amount
    functions.httpsCallable("stripeCharge").call(["token": token.tokenId, "amount": String(stripeAmount)]) { (result, error) in
        if let error = error {
            print("Error: \(error)")
        }

        // Get the charge id after successful payment
        var chargeId: String
        if let data = result?.data as? [String: Any] {
            chargeId = data["chargeId"] as? String ?? "no id"
            print("Charge id: \(chargeId)")

            //send new info

            //show successfull payment view with charge

            //self.present(self.successViewController, animated: true, completion: nil)
            self.performSegue(withIdentifier: "successView", sender: self)
        }

        completion(nil)
        //self.performSegue(withIdentifier: "successView", sender: self)
    }

}

但是我不断收到错误消息“试图在其视图不在窗口层次结构中的...上呈现...”

有人知道为什么会这样吗?这是main.storyboard的图片 here is a picture of the main.storyboard

1 个答案:

答案 0 :(得分:0)

可能是您不在主线程上?通常,网络调用的回调函数不在主线程中。除非您确定这不是问题,否则请尝试添加它:

DispatchQueue.main.async {
  self.performSegue(withIdentifier: "successView", sender: self)
}