在我的应用程序中,SubscriptionVC 中有一个按钮,当按下该按钮时,它将调用 IAPService 类中的“购买”函数。但功能完成后,我希望使用 ViewController 的 switchState() 方法更改 ViewController 中的元素。有谁知道该怎么做?
class SubscriptionVC: UIViewController{
@IBOutlet weak var slideBtn: UISegmentedControl!
@IBOutlet weak var testLbl: UILabel!
func switchSubscribeState(){
testLbl.text = “You have changed this label”
slideBtn.isHidden=true
}
@IBAction func touchSubscribeBtn(_ sender: Any) {
TestService.shared.buttonPushed( subsriptionVC:self, product: prod)
}
class IAPService: NSObject {
private override init() {}
static let shared = IAPService()
var currentViewController: SubscriptionVC?
func purchase(subsriptionVC: SubscriptionVC ) {
currentViewController = subsriptionVC
guard let productToPurchase = products.filter({ $0.productIdentifier == product.rawValue}).first else {return}
let payment = SKPayment(product: productToPurchase)
print("payment",product.rawValue)
paymentQueue.add(payment)
}
}
extension IAPService: SKPaymentTransactionObserver {
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .failed, .deferred:
SKPaymentQueue.default().finishTransaction(transaction)
SKPaymentQueue.default().remove(self)
break
case .purchasing:
break
case .purchased,.restored:
//Call switchState() when purchased
subVC?.switchSubscribeState()
SKPaymentQueue.default().finishTransaction(transaction)
SKPaymentQueue.default().remove(self)
break
default:
SKPaymentQueue.default().finishTransaction(transaction)
SKPaymentQueue.default().remove(self)
}
}
}
}