视图控制器之间如何交换数据?

问题描述 投票:0回答:1

我有

PurchaseViewController
,带有购买按钮。如果用户点击
ParentsVerificationController
中的按钮之一,我需要向控制器显示一个简单的父级检查 (
PurchaseViewController
)。如果
ParentsVerificationController
中的验证成功完成,我需要返回
PurchaseViewController
并完成购买。我使用这个代码:

PurchaseViewController:

verificationState == false

@objc func weeklyButtonAction() {
        let detailController = SLVerification()
        detailController.modalPresentationStyle = .overFullScreen
        self.present(detailController, animated: false, completion: nil)
        
        if verificationState == true {
            print("good")
        }
    }
    
    @objc func monthlyButtonAction() {
        let detailController = SLVerification()
        detailController.modalPresentationStyle = .overFullScreen
        self.present(detailController, animated: false, completion: nil)
        
        if verificationState == true {
            print("good")
        }
    }
    
    @objc func annualButtonAction() {
        let detailController = SLVerification()
        detailController.modalPresentationStyle = .overFullScreen
        self.present(detailController, animated: false, completion: nil)
        
        if verificationState == true {
            print("good")
        }
    }

ParentsVerificationController:

@objc func buttonAction(sender: SLButton!) {
        switch (sender.tag) {
        case 0: if buttonsArray[0] == a {
            print("success")
            let presenter = PurchaseController()
            presenter.verificationState = true
            dismissVerification()
        } else {
            print("failure")
            shakeAnimation()
            dismissVerification()
        }
        case 1: if buttonsArray[1] == a {
            print("success")
            let presenter = PurchaseController()
            presenter.verificationState = true
            dismissVerification()
        } else {
            print("failure")
            shakeAnimation()
            dismissVerification()
        }
        case 2: if buttonsArray[2] == a {
            print("success")
            let presenter = PurchaseController()
            presenter.verificationState = true
            dismissVerification()
        } else {
            print("failure")
            shakeAnimation()
            dismissVerification()
        }
        case 3: if buttonsArray[3] == a {
            print("success")
            let presenter = PurchaseController()
            presenter.verificationState = true
            dismissVerification()
        } else {
            print("failure")
            shakeAnimation()
            dismissVerification()
        }
        default: print("error")
        }
    }

我返回到PurchaseViewController并显示“成功”,但

weeklyButtonAction
monthlyButtonAction
annualButtonAction
中的if else语句不会调用。我看不到“好”。怎么解决?

swift uikit
1个回答
0
投票

每次调用

buttonAction
时,您都会创建一个新的
PurchaseController
,因此,您不会通知旧
PurchaseViewController
的更改。 要解决您的问题,我建议使用
delegate pattern
:

class ParentsVerificationController {
    weak var delegate: ParentsVerificationControllerDelegate?
    @objc func buttonAction(sender: SLButton!) {
        switch (sender.tag) {
        case 0:
            if buttonsArray[0] == a {
                delegate?.didVerificationSuccess
                dismissVerification()
            } else {
                shakeAnimation()
                dismissVerification()
            }
            ...
        }
    }
}


class PurchaseViewController {
    @objc func weeklyButtonAction() {
        let detailController = ParentsVerificationController()
        detailController.delegate = self
        detailController.modalPresentationStyle = .overFullScreen
        self.present(detailController, animated: false, completion: nil)
        
        if verificationState == true {
            print("good")
        }
    }
}

extension PurchaseViewController: ParentsVerificationControllerDelegate {
    func didVerificationSuccess() {
        print("good")
    }
    
}
© www.soinside.com 2019 - 2024. All rights reserved.