Xcode 应用程序问题从另一个类访问我的 ViewController 中的元素?

问题描述

在我的应用程序中,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)
        }
       
    }
}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)