如何通过NSNotification将消息从一个类传递到另一个类

时间:2012-03-08 05:43:24

标签: iphone ios nsnotifications

Ha-ii,我正在我的应用程序中进行应用内购买,我需要在应用内购买成功时禁用按钮,因此我将NSuserdefault置于我的代码中以识别天气是否成功,但NSUserDefault仅在我刷新页面时才有效,当内容成功消息到来时我没有启用按钮,当我回到另一个页面然后回来时我启用了,我需要通过通知将实例消息传递给viewcontroller .my代码是

- (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值。 怎么做。请帮助我。

1 个答案:

答案 0 :(得分:2)

在您的第一个视图vieDidLoad中添加该行..

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

(请删除dealloc中的观察者)

然后当您在问题代码中成功购买时.. 发布通知

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

您可以传递一个对象 ..但我相信您应该将购买交易明细存储在NSUserdefautls ..因为它将在下次运行应用时需要.. < / p>

当您发布时,在第一个视图中调用通知 ContentPurchased方法(您必须在视图控制器中添加该方法)

相关问题