我在My ViewController中使用以下代码,但是当我调用
_priceFormatter
时,它会显示价格为(null)
。 [buyButton setTitle:[NSString stringWithFormat:@"Upgrade for %@", [_priceFormatter stringFromNumber:product.price]] forState:UIControlStateNormal];
ViewController.m
{
NSNumberFormatter * _priceFormatter;
}
ViewDidLoad
[RageIAPHelper sharedInstance];
_products = nil;
[[RageIAPHelper sharedInstance] requestProductsWithCompletionHandler:^(BOOL success, NSArray *products) {
if (success) {
_products = products;
}
}];
SKProduct * product = (SKProduct *) [_products objectAtIndex:0];
([[RageIAPHelper sharedInstance] productPurchased:product.productIdentifier]);
_priceFormatter = [[NSNumberFormatter alloc] init];
[_priceFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[_priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[_priceFormatter setLocale:product.priceLocale];
[_priceFormatter stringFromNumber:product.price];
编辑过的
_priceFormatter = [[NSNumberFormatter alloc] init];
[_priceFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[_priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[_priceFormatter setLocale:product.priceLocale];
NSString *priceString = [_priceFormatter stringFromNumber:product.price];
按钮:
UIButton *buyButton = [[UIButton alloc] initWithFrame:CGRectMake(-1, 370, 320, 60)];
[buyButton setTitle:[NSString stringWithFormat:@"Upgrade for %@", priceString] forState:UIControlStateNormal];
[buyButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
[buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[buyButton.titleLabel setShadowColor:[UIColor colorWithWhite:0.1 alpha:1.0]];
[buyButton.titleLabel setShadowOffset:CGSizeMake(0, -1)];
[buyButton addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
buyButton.tag = 0;
[[self view] addSubview:buyButton];
requestProductsWithCompletionHandler
是异步的。你试图在它被设置之前访问_products
。 - rmaddyproductPurchased
,而不是仅仅显示按钮时就调用。 - rmaddy