簡體   English   中英

如何通過NSNotification將消息從一個類傳遞到另一個類

[英]How to pass messages from one class to another through NSNotification

Ha-ii,我正在我的應用程序中進行應用內購買,我需要在應用內購買成功時禁用按鈕,因此我將NSuserdefault置於我的代碼中以識別天氣是否成功,但NSUserDefault正常工作只有當我刷新頁面時,我沒有在內容成功消息到來時啟用按鈕,當我回到另一個頁面然后回來時我啟用了,我需要通過notification.my代碼將實例消息傳遞給viewcontroller是

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for(SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {

            case SKPaymentTransactionStatePurchasing:
                // Item is still in the process of being purchased
                break;

            case SKPaymentTransactionStatePurchased:
                // Item was successfully purchased!

                // --- UNLOCK FEATURE OR DOWNLOAD CONTENT HERE ---
                // The purchased item ID is accessible via 
                // transaction.payment.productIdentifier

                // After customer has successfully received purchased content,
                // remove the finished transaction from the payment queue.
                [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
                 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Transsucess"];
                break;

            case SKPaymentTransactionStateRestored:
                // Verified that user has already paid for this item.
                // Ideal for restoring item across all devices of this customer.

                // --- UNLOCK FEATURE OR DOWNLOAD CONTENT HERE ---
                // The purchased item ID is accessible via 
                // transaction.payment.productIdentifier

                // After customer has restored purchased content on this device,
                // remove the finished transaction from the payment queue.
                [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
                break;

            case SKPaymentTransactionStateFailed:
                // Purchase was either cancelled by user or an error occurred.

                if (transaction.error.code != SKErrorPaymentCancelled) {
                    // A transaction error occurred, so notify user.
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Transsucess"];
                }
                // Finished transactions should be removed from the payment queue.
                [[SKPaymentQueue defaultQueue] finishTransaction: 

transaction];
                break;
        }
    }
}

在我的anothervierwcontroller中,我想傳遞Nsuserdefault值的Notification值。 怎么做。請幫助我。

在你的第一個視圖中vieDidLoad添加行..

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ContentPurchased) name:@"Purchased" object:nil];

(在dealloc中刪除觀察者)

然后當您在問題代碼中成功購買時。發布通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"Purchased"  object:nil userInfo:nil];

你可以傳遞一個對象 ..但我相信你應該在NSUserdefautls存儲購買交易細節NSUserdefautls因為它將在下一次運行應用程序時需要。

當您發布 通知時,在第一個視圖中調用ContentPurchased方法(您必須在視圖控制器中添加它)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM