我在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];
EDITED
_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];
答案 0 :(得分:6)
您需要等到调用完成处理程序才能处理产品:
[RageIAPHelper sharedInstance];
_products = nil;
[[RageIAPHelper sharedInstance] requestProductsWithCompletionHandler:^(BOOL success, NSArray *products) {
if (success) {
_products = products;
SKProduct * product = _products[0];
[[RageIAPHelper sharedInstance] productPurchased:product.productIdentifier];
_priceFormatter = [[NSNumberFormatter alloc] init];
[_priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[_priceFormatter setLocale:product.priceLocale];
NSString *price = [_priceFormatter stringFromNumber:product.price];
someLabel.text = price;
}
}];
正如您所看到的,您需要实际使用格式化的数字字符串。你只是丢掉了价值。