removeFromSuperview无法重新打开

时间:2015-02-02 15:41:19

标签: ios objective-c xcode button uiview

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

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

我还应该添加和/或更改代码,以便再次单击“购买”按钮时,它将再次打开viewInAppPurchases?

在页眉文件中:

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

在实现.m文件中:

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

以下是作为GaryRiches请求的IAP。我注意到我没有一个"关闭按钮" 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

2 个答案:

答案 0 :(得分:0)

  

我还应该在购买时添加和/或更改代码   再次单击按钮,它将打开viewInAppPurchases   再次?

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

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

[self.view addSubview: _viewInAppPurchases];

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

隐藏您的观点:

[_viewInAppPurchases setHidden:YES];

再次展示:

[_viewInAppPurchases setHidden:NO];

答案 1 :(得分:0)

我改变了

removeFromSuperview

setHidden:Yes

它就行了。