为什么这些NSNotifications会重复?

时间:2012-03-27 05:56:22

标签: iphone objective-c nsnotifications

我正在进行应用程序购买部分,该部分具有通知设置,该通知设置将通知从观察者类传递到主类。我有这个代码

- (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


                [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_DataComplete object:nil] ;
                 [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_DataCompletehindi object:nil];
                 [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_DataCompletetamil object:nil];
                 [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_DataCompletetelugu object:nil];
                 [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_DataCompletekannada object:nil];
                // After customer has successfully received purchased content,
                // remove the finished transaction from the payment queue.
                [[SKPaymentQueue defaultQueue] finishTransaction: transaction];


            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.
                [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_Datacomplteing object:nil];
                if (transaction.error.code != SKErrorPaymentCancelled) {
}
                // Finished transactions should be removed from the payment queue.
                [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
                break;
        }
    }
}

我将此通知传递给我的主要课程,但我收到了重复通知。每次都会重复通知,但是我没有从与通知名称对应的观察者类中获得正确的通知。我该如何解决这个问题。

编辑: 我有五个这样的警报视图:

if
{
}
elseif
{
}
else if
{
}
else if(alertView == kannadaPurchasedAlert)
{
    if (buttonIndex==0)
    {                        
        NSString *connectionstring =  [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];

        if ([connectionstring length]==0) {
            proAlertView *alert = [[proAlertView alloc]initWithTitle:nil message:@"you are not connected to the internet" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
            [alert setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0] withStrokeColor:[UIColor colorWithHue:0.0 saturation:0.0 brightness:0.0 alpha:1.0]];
            [alert show];
            [alert release];
   }
   else
   { 
        [NSThread detachNewThreadSelector: @selector(spinBegininapp) toTarget:self withObject:nil];
        // Replace "Your IAP Product ID" with your actual In-App Purchase Product ID.
        SKPayment *paymentRequest = [SKPayment paymentWithProductIdentifier: @"com.touch.MyApp.MyApp.Kannada"]; 

        // Assign an Observer class to the SKPaymentTransactionObserver,
        // so that it can monitor the transaction status.
        [[SKPaymentQueue defaultQueue] addTransactionObserver:inappObserver];

        // Request a purchase of the selected item.
        [[SKPaymentQueue defaultQueue] addPayment:paymentRequest];
        // Register observer for when download of data is complete
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadDataCompletekannada:) name:NOTIF_DataCompletekannada object:nil]; 
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadDataCompleting:) name:NOTIF_Datacomplteing object:nil]; 
        amtk = 0.0;                         
   } 
}

然后在.h文件中 我把

extern NSString * const NOTIF_DataComplete;
extern NSString * const NOTIF_Datacomplteing;
extern NSString * const NOTIF_FIRSTLAUNCH;
extern NSString * const NOTIF_DataCompletehindi;
extern NSString * const NOTIF_DataCompletetamil;
extern NSString * const NOTIF_DataCompletetelugu;
extern NSString * const NOTIF_DataCompletekannada;

2 个答案:

答案 0 :(得分:0)

您的NOTIF_DataCompleteXXX在哪里宣布?如果你没有为它们分配任何字符串,它们都是相同的“空命名”通知,每一个都会触发所有观察者。

答案 1 :(得分:0)

问题可能在于addObservers。这些是累积的,这意味着每次调用它时,通知中心都会注册一个新的回调。如果在方法中一次调用它,请确保仅调用该方法一次。例如,viewDidAppear:方法可能会在视图控制器的生命周期中被多次调用。最好在viewDidLoad中设置观察者并在dealloc方法中释放它们。