You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS视图控制器间数据传递问题:验证成功后未触发后续购买逻辑的修复

iOS视图控制器间数据传递问题:验证成功后未触发后续购买逻辑的修复

你现在的代码有两个核心问题导致“good”永远打不出来,我来给你拆解并提供修复方案:

问题1:逻辑执行顺序完全错了

你点击购买按钮后,立刻present验证控制器,紧接着就判断verificationState == true——这时候验证还没开始呢!用户还没在验证页面操作,verificationState肯定是初始的false,所以这个判断代码等于白写,完全起不到等待验证结果的作用。

问题2:修改了错误的控制器实例

在ParentsVerificationController的成功分支里,你新建了一个PurchaseController对象:

let presenter = PurchaseController()
presenter.verificationState = true

但这个presenter是全新的对象,和你之前present验证控制器的那个原PurchaseViewController完全不是同一个!你修改的是新对象的状态,原控制器的verificationState根本没变化,自然也不会触发任何后续逻辑。


最适合的修复方案:用闭包做回调(简单直接)

这种场景下用闭包回调是最轻便的实现方式,不用复杂的代理协议,代码量也少:

第一步:修改ParentsVerificationController(SLVerification)

先给它加一个闭包属性,用来通知验证成功的事件:

// 定义成功回调闭包
var onVerificationSuccess: (() -> Void)?

@objc func buttonAction(sender: SLButton!) {
    switch (sender.tag) {
    case 0: 
        if buttonsArray[0] == a {
            print("success")
            // 验证成功时调用闭包
            onVerificationSuccess?()
            dismissVerification()
        } else {
            print("failure")
            shakeAnimation()
            dismissVerification()
        }
    case 1: 
        if buttonsArray[1] == a {
            print("success")
            onVerificationSuccess?()
            dismissVerification()
        } else {
            print("failure")
            shakeAnimation()
            dismissVerification()
        }
    case 2: 
        if buttonsArray[2] == a {
            print("success")
            onVerificationSuccess?()
            dismissVerification()
        } else {
            print("failure")
            shakeAnimation()
            dismissVerification()
        }
    case 3: 
        if buttonsArray[3] == a {
            print("success")
            onVerificationSuccess?()
            dismissVerification()
        } else {
            print("failure")
            shakeAnimation()
            dismissVerification()
        }
    default: print("error")
    }
}

第二步:修改PurchaseViewController的按钮点击事件

把每个按钮的点击逻辑改成先present验证控制器,再通过闭包接收成功通知,不用再提前判断状态:

// 先把重复的代码抽出来,避免冗余
private func presentVerificationAndHandleSuccess(completion: @escaping () -> Void) {
    let detailController = SLVerification()
    detailController.modalPresentationStyle = .overFullScreen
    detailController.onVerificationSuccess = completion
    self.present(detailController, animated: false, completion: nil)
}

@objc func weeklyButtonAction() {
    presentVerificationAndHandleSuccess { [weak self] in
        guard let self = self else { return }
        self.verificationState = true
        print("good")
        // 在这里执行周订阅的购买逻辑
    }
}

@objc func monthlyButtonAction() {
    presentVerificationAndHandleSuccess { [weak self] in
        guard let self = self else { return }
        self.verificationState = true
        print("good")
        // 在这里执行月订阅的购买逻辑
    }
}

@objc func annualButtonAction() {
    presentVerificationAndHandleSuccess { [weak self] in
        guard let self = self else { return }
        self.verificationState = true
        print("good")
        // 在这里执行年订阅的购买逻辑
    }
}

为什么这个方案能解决问题?

  1. 逻辑顺序正确了:只有当用户在验证页面操作成功后,闭包才会被调用,这时候才执行print("good")和购买逻辑,完全符合你的业务流程。
  2. 操作的是原控制器实例:闭包是绑定在原PurchaseViewController上的,修改的是原控制器的verificationState,不会出现“修改新对象状态”的问题。
  3. 代码更简洁:通过抽出来的presentVerificationAndHandleSuccess方法,避免了三个按钮点击事件的重复代码,后续维护也更方便。

备注:内容来源于stack exchange,提问作者Alex Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.16 02:54:40