【问题标题】:removeFromSuperview can't reopenremoveFromSuperview 无法重新打开
【发布时间】:2015-02-02 15:41:19
【问题描述】:

我有一个按钮(购买)可以在另一个 UIView (viewMenu) 之上打开一个 UIView (viewInAppPurchases)。 Buy 按钮位于 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

【问题讨论】:

  • 您能否也显示显示 IAP 视图的代码,因为那是无效的。
  • 如果您从视图中删除文件,您还必须将其添加回来...

标签: ios objective-c xcode button uiview


【解决方案1】:

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

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

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

[self.view addSubview: _viewInAppPurchases];

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

隐藏你的视图:

[_viewInAppPurchases setHidden:YES];

再次显示:

[_viewInAppPurchases setHidden:NO];

【讨论】:

  • 是的,我也考虑过隐藏它。我不知道该怎么做,因为我对 Xcode 很陌生。
【解决方案2】:

我改了

removeFromSuperview

setHidden:Yes

它成功了。

【讨论】:

    猜你喜欢
    • 2014-07-26
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多