UIButton没有回应触摸

时间:2013-12-17 16:36:14

标签: ios objective-c uibutton

我正在尝试这样做,以便如果我在我的应用程序上购买项目,它将允许我隐藏我放置的iAd。
但是,当我测试时,我的恢复按钮是可以点击的,我无法点按购买按钮。

我的应用程序错过了哪些内容?

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSUserDefaults *saveapp = [NSUserDefaults standardUserDefaults];
    bool saved = [saveapp boolForKey:k_Save];
    if (!saved) {
        /// not save code here
    } else {
        ///saved code here
        Label.text = @"item has been purchased";
    }
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
    NSUserDefaults *saveapp = [NSUserDefaults standardUserDefaults];
    bool saved = [saveapp boolForKey:k_Save];
    if (!saved) {
        /// not save code here
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1];
        [banner setAlpha:1];
        [UIView commitAnimations];
    } else {
        ///saved code here
    }
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [banner setAlpha:0];
    [UIView commitAnimations];
}

- (IBAction)PurchaseItem:(id)sender {
    _purchaseController = [[PurchasedViewController alloc] initWithNibName:nil bundle:nil];
    _purchaseController.productID = @"com.myname.appname.iap1";
    [[SKPaymentQueue defaultQueue] addTransactionObserver:_purchaseController];
    [self presentViewController:_purchaseController animated:YES completion:NULL];
    [_purchaseController getProductID:self];
}

-(void)Purchased {
    Label.text = @"item has been purchased";
    iadBanner.hidden = YES;
    NSUserDefaults *saveapp = [NSUserDefaults standardUserDefaults];
    [saveapp setBool:TRUE forKey:k_Save];
    [saveapp synchronize];
}

购买视图

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _buyButton.enabled = NO;
}

- (IBAction)BuyProduct:(id)sender {
    SKPayment *payment = [SKPayment paymentWithProduct:_product];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

- (IBAction)Restore:(id)sender {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
    [self UnlockPurchase];
}

-(void)getProductID:(ViewController *)viewController {
    _homeViewController = viewController;
    if ([SKPaymentQueue canMakePayments]) {
        SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:self.productID]];
        request.delegate = self;
        [request start];
    } else
        _productDescription.text = @"Please enable in app purchase in your settings";
}

#pragma mark _
#pragma mark SKProductsRequestDelegate

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    NSArray *products = response.products;
    if (products.count != 0) {
        _product = products[0];
        _buyButton.enabled = YES;
        _productTitle.text = _product.localizedTitle;
        _productDescription.text = _product.localizedDescription;
    } else {
        _productTitle.text = @"Product Not Found";
    }
    products = response.invalidProductIdentifiers;
    for (SKProduct *product in products) {
        NSLog(@"Product not Found: %@", product);
    }
}

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchased:[self UnlockPurchase];
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:NSLog(@"Transaction Failed");
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            default:
                break;

        }
    }
}

-(void)UnlockPurchase {
    _buyButton.enabled = NO;
    [_buyButton setTitle:@"Purchased" forState:UIControlStateDisabled];
    [_homeViewController Purchased];
}

1 个答案:

答案 0 :(得分:3)

您使用_buyButton.enabled = NO;上的viewDidLoad明确禁用它,并且仅在您收到产品时将其设置为启用..您只能通过首先点击按钮来执行此操作。