0
這裏就是我稱之爲:enumerateObjectsUsingBlock:未執行
/**
* called when we are notified that a product has been purchased
*
* @param notification the notification object calling this method
*/
- (void)productPurchased:(NSNotification *)notification
{
// the notification object is purchased product identifier
NSString *productIdentifier = notification.object;
NSLog(@"Product purchased");
// reload appropriate cell with checkmark
[_products enumerateObjectsUsingBlock:^(SKProduct *product, NSUInteger idx, BOOL *stop)
{
NSLog(@"Block executing with product: %@", product.productIdentifier);
NSLog(@"Needs to match: %@", productIdentifier);
if ([product.productIdentifier isEqualToString:productIdentifier])
{
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:idx inSection:0]]
withRowAnimation:UITableViewRowAnimationFade];
NSLog(@"Reloading due to purchase");
*stop = YES;
}
}];
}
然而,該塊永遠不會被調用,因此表格視圖單元格是永遠不會被重新加載。我可以告訴塊永遠不會執行,因爲日誌永遠不會顯示在控制檯中。但是,通過放置一個斷點,我可以確定productPurchased:方法肯定會被調用。
任何幫助表示讚賞。
謝謝。
編輯:我現在已經改變了上面的代碼,以允許在執行過程中有更多的日誌記錄。從我已經能夠通過查看控制檯收集,在傳遞的通知對象是表視圖本身:
<UITableView: 0x1eaffe00; frame = (0 0; 320 704); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1e57dfe0>; layer = <CALayer: 0x1e57da50>; contentOffset: {0, 0}>
這顯然是具有這種功能的話的問題,所以我會調查更進一步,並儘快發佈我的答案。
現在我將包括如何這個功能應該被稱爲:
簽約通知:
/**
* notifies the view controller that its view is about to be added to a view hierarchy
*
* @param animated whether it will be added in an animated fashion
*/
- (void)viewWillAppear:(BOOL)animated
{
// add ourselves as an observer to product purchases
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productPurchased:) name:IAHelperProductPurchasedNotification object:nil];
}
發送通知:
/**
* handles all the extra work not that a product has been purchased
*
* @param productIdentifier the purchased product's identifier
*/
- (void)provideContentForProductIdentifier:(NSString *)productIdentifier
{
// adds the product's id to our purchased product ids list
[_purchasedProductIdentifiers addObject:productIdentifier];
// stores this purchase in nsuserdefaults for long term use
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:productIdentifier];
// save the user defaults
[[NSUserDefaults standardUserDefaults] synchronize];
// send notification to tohers for awareness of this purchase
[[NSNotificationCenter defaultCenter] postNotificationName:IAHelperProductPurchasedNotification object:productIdentifier userInfo:nil];
}
您是否檢查_products是否爲空或空? – Eiko
你是否在塊內部放置了'NSLog'語句,但是在'if'語句之外。 – mttrb
這似乎是一個higg錯誤的兒子。做一個測試,並嘗試在方法調用開始時獲得nslog的輸出。如果你沒有得到它,那麼Xcode編譯你的應用程序,但不要安裝它,你會得到你舊版本的舊版痕跡,而gdb會工作,但我只是猜測。 –