У меня есть кнопка (Купить), чтобы открыть UIView (viewInAppPurchases) поверх другого UIView (viewMenu). Кнопка «Купить» находится в режиме просмотра UIViewMenu. У меня также есть кнопка (Закрыть) в UIView viewInAppPurchases.removeFromSuperview не может открыть повторно
В коде при нажатии кнопки закрытия в viewInAppPurchases он должен закрыть представление и вернуться к viewMenu. Но при повторном нажатии кнопки «Купить» ничего не происходит. Он должен снова открыть UIView viewInAppPurchases.
Что еще я должен добавить и/или изменить код, поэтому, когда кнопка «Купить» снова нажата, она снова откроет viewInAppPurchases?
В файле заголовка:
@property (strong, nonatomic) IBOutlet UIView *viewInAppPurchases;
В реализации файл .m:
- (IBAction)buttonClose: (UIButton*)sender
{
[_viewInAppPurchases removeFromSuperview];
}
Ниже IAP как запрос GaryRiches. Я заметил, что у меня не было инструкции «закрыть кнопку» в самой IAP, но только через MenuViewController.
Как поместить в него инструкции кнопки закрытия?
BuyView.h
#import <UIKit/UIKit.h>
#import <Social/Social.h>
#import "MyIAPHelper.h"
@interface BuyView : UIView
{
SKProduct *product;
NSMutableArray *lstProducts;
int productIndex;
BOOL hasProducts;
}
@property (nonatomic, strong) UIViewController *parentViewController;
- (IBAction)buyCoin:(id)sender;
- (IBAction)shareTwitterGetCoin:(id)sender;
- (IBAction)shareFacebookGetCoin:(id)sender;
@end
BuyView.m
#import "BuyView.h"
@implementation BuyView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(productPurchased:)
name:IAPHelperProductPurchasedNotification object:nil];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
NSArray *nibs = [[NSBundle mainBundle]
loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
// NSArray *subviewArray = [[NSBundle mainBundle]
loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
UIView *mainView = [nibs objectAtIndex:0];
//Just in case the size is different (you may or may not want this)
mainView.frame = self.bounds;
[self addSubview:mainView];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(productPurchased:)
name:IAPHelperProductPurchasedNotification object:nil];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during
animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (IBAction)buyCoin:(id)sender {
BUTTON_SOUND
UIButton *btn = (UIButton *)sender;
NSString *productID = @"";
switch (btn.tag) {
case 0:
NSLog(@"buy 0.99");
productIndex = 0;
productID = FIRST_TIER;
break;
case 1:
NSLog(@"buy 1.99");
productIndex = 1;
productID = SECOND_TIER;
break;
case 2:
NSLog(@"buy 4.99");
productIndex = 2;
productID = THIRD_TIER;
break;
case 3:
NSLog(@"buy 9.99");
productIndex = 3;
productID = FOURTH_TIER;
break;
case 4:
NSLog(@"buy 19.99");
productIndex = 4;
productID = FIFTH_TIER;
break;
default:
break;
}
SKProduct *selectedProduct;
for (int i=0; i<lstProducts.count; i++) {
SKProduct *_product = [lstProducts objectAtIndex:i];
if ([_product.productIdentifier isEqualToString:productID]) {
selectedProduct = _product;
break;
}
}
[[MyIAPHelper shareInstance] buyProduct:selectedProduct];
}
- (void)productPurchased:(NSNotification *)notification {
NSLog(@"===========PurchaseViewController===========");
NSLog(@"purchased success");
//Add coin here IAPHelperProductPurchasedNotification
int increaCoin = 0;
switch (productIndex) {
case 0:
increaCoin = 100;
break;
case 1:
increaCoin = 250;
break;
case 2:
increaCoin = 750;
break;
case 3:
increaCoin = 2000;
break;
case 4:
increaCoin = 5000;
break;
default:
break;
}
NSString *strMsg = [NSString stringWithFormat:@"You have purchased
successfully and got %d coins",increaCoin];
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Message" message:strMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[self updateCoinWithIncreaseCoin:increaCoin];
}
- (void)updateCoinWithIncreaseCoin:(int)increaseCoin_{
int currentCoin = [Utils getCoin];
[Utils updateCoin:(currentCoin + increaseCoin_)];
}
- (IBAction)shareTwitterGetCoin:(id)sender {
BUTTON_SOUND
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
if (![userdefaults objectForKey:@"FIRST_SHARE_TWITTER"]) {
[userdefaults setObject:@"Abcd" forKey:@"FIRST_SHARE_TWITTER"];
[userdefaults synchronize];
SLComposeViewController *controller = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[controller setInitialText:@"Check out this new App!"];
[controller addURL:[NSURL URLWithString:APP_URL]];
[self.parentViewController presentViewController:controller
animated:YES completion:^{
//get coin here
NSLog(@"get coin share twitter");
int currentCoin = [Utils getCoin] + 30;
[Utils updateCoin:currentCoin];
}];
}
}
- (IBAction)shareFacebookGetCoin:(id)sender {
BUTTON_SOUND
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
if (![userdefaults objectForKey:@"FIRST_SHARE_FACEBOOK"]) {
[userdefaults setObject:@"Abc" forKey:@"FIRST_SHARE_FACEBOOK"];
[userdefaults synchronize];
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:@"Check out this new App!"];
[controller addURL:[NSURL URLWithString:APP_URL]];
[self.parentViewController presentViewController:controller animated:YES completion:^{
//add coin here
NSLog(@"get coin share facebook");
int currentCoin = [Utils getCoin] + 30;
[Utils updateCoin:currentCoin];
}];
}else{
}
}
@end
Не могли бы вы также показать код, отображающий вид IAP, поскольку это тот, который не работает. –
Если вы удалите файл из представления, вы также должны добавить его обратно ... – Miknash