在应用程序购买 - 如何制作"是"按钮是"购买"

时间:2014-03-23 23:23:42

标签: objective-c tags uibutton in-app-purchase uialertview

我想将UIAlertView中的“是”按钮重命名为“购买”。

以下是代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

SKProduct * product = (SKProduct *) _products[indexPath.row];
cell.textLabel.text = product.localizedTitle;


[_priceFormatter setLocale:product.priceLocale];
cell.detailTextLabel.text = [_priceFormatter stringFromNumber:product.price];

if ([[RageIAPHelper sharedInstance] productPurchased:product.productIdentifier]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.accessoryView = nil;
} else {
    UIButton * buyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buyButton.frame = CGRectMake(0, 0, 72, 37);
    [buyButton setTitle:@"Buy" forState:UIControlStateNormal];
    buyButton.tag = indexPath.row;
    [buyButton addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.accessoryView = buyButton;
}

return cell;
}


- (void)buyButtonTapped:(id)sender {

UIButton * buyButton = (UIButton *)sender;
SKProduct *product = _products[buyButton.tag];

NSLog(@"Buying %@...", product.productIdentifier);
[[RageIAPHelper sharedInstance] buyProduct:product];

}

到目前为止,单元格中的“购买”操作确实有效。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];

if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
    UIStoryboard    *   storyboard  =   [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    SSDetailViewController * detailViewController   =   [storyboard instantiateViewControllerWithIdentifier:@"detail"];
    [self.navigationController pushViewController:detailViewController animated:YES];
} else {
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Cell Selected to Buy"
                                                         message:@"Tap \"YES\" to Confirm Purchase"
                                                        delegate:self
                                               cancelButtonTitle:@"Cancel"
                                               otherButtonTitles:@"Yes", nil];
    [alertView show];


}
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

if (buttonIndex == 0) {
    NSLog(@"cancel");
} else {
    NSLog(@"YES");

 // **here want to make "buy" operation, but how to get `button.tag` that in the tapped cell?**      
 //SKProduct *product = _products[button.tag];
 //[[RageIAPHelper sharedInstance] buyProduct:product];
}
}

这里有两张图片供参考,谢谢任何能帮我一把的人!

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

UIView的标签不适合传递信息,尽管你看到这种滥用很多。

保存所选单元格的索引路径,并在触发警报按钮时使用该路径。按钮上的名字并不重要,只需给它另一个标题。