removeFromSuperview 无法重新打开

removeFromSuperview can't reopen

我有一个按钮(购买),用于在另一个 UIView (viewMenu) 之上打开一个 UIView (viewInAppPurchases)。 Buy 按钮位于 UIView viewMenu 中。我在 UIView viewInAppPurchases 中也有一个按钮(关闭)。

在代码中,当点击viewInAppPurchases 中的关闭按钮时,将关闭视图并返回到viewMenu。但是当再次点击购买按钮时,没有任何反应。它应该重新打开 viewInAppPurchases UIView。

我还应该添加什么 and/or 更改代码,以便再次单击“购买”按钮时,它会再次打开 viewInAppPurchases?

在头文件中:

@property (strong, nonatomic) IBOutlet UIView *viewInAppPurchases;

正在执行.m文件:

- (IBAction)buttonClose: (UIButton*)sender
{
[_viewInAppPurchases removeFromSuperview];
}

以下是作为 GaryRiches 请求的 IAP。我注意到我在 IAP 本身中没有 "close button" 指令,但只能通过 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

What else should I add and/or change to the code so when the Buy button is clicked again, it shall open up the viewInAppPurchases again?

您应该将它添加回您从中删除它的视图。

您可以通过以下方式在点击事件中执行此操作:

[self.view addSubview: _viewInAppPurchases];

此外,您是否考虑过隐藏它而不是完全删除它然后再添加回来?

隐藏你的观点:

[_viewInAppPurchases setHidden:YES];

再次展示:

[_viewInAppPurchases setHidden:NO];

我改变了

removeFromSuperview

setHidden:Yes

它成功了。