需要删除子视图

时间:2014-09-24 16:54:48

标签: ios objective-c iphone

我用这个叫做横幅;

[self.view addSubview:[[AdManager sharedInstance] adMobBanner]];

从AdManager.m文件调用;

- (GADRequest *)adMobrequest {
    GADRequest *request = [GADRequest request];
    request.testDevices = @[
                            // TODO: Add your device/simulator test identifiers here. Your device identifier is printed to
                            // the console when the app is launched.
                            GAD_SIMULATOR_ID,
                            ];
    return request;
}

-(GADBannerView*)adMobBannerWithAdUnitID:(NSString*)adUnitID{
    GADBannerView *bannerView = [[GADBannerView alloc] initWithAdSize:GADAdSizeFromCGSize(CGSizeMake([UIScreen mainScreen].bounds.size.width, AdmobBannerHeight)) origin:CGPointMake(0,
                                                                                                                                                                                     [UIScreen mainScreen].bounds.size.height -AdmobBannerHeight)];
    bannerView.adUnitID = adUnitID;
    NSString *sourceString = [[NSThread callStackSymbols] objectAtIndex:1];
    // Example: 1   UIKit                               0x00540c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
    NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"];
    NSMutableArray *array = [NSMutableArray arrayWithArray:[sourceString  componentsSeparatedByCharactersInSet:separatorSet]];
    [array removeObject:@""];

    bannerView.rootViewController = [array objectAtIndex:3];
    [bannerView loadRequest:[[AdManager sharedInstance] adMobrequest]];

    return bannerView;
}


-(GADBannerView*)adMobBanner{
    GADBannerView *bannerView = [[GADBannerView alloc] initWithAdSize:GADAdSizeFromCGSize(CGSizeMake([UIScreen mainScreen].bounds.size.width, AdmobBannerHeight)) origin:CGPointMake(0,
                                                                                                                                                                                     [UIScreen mainScreen].bounds.size.height -AdmobBannerHeight)];
    bannerView.adUnitID = ADMOB_BANNER_ID;
    NSString *sourceString = [[NSThread callStackSymbols] objectAtIndex:1];
    // Example: 1   UIKit                               0x00540c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
    NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"];
    NSMutableArray *array = [NSMutableArray arrayWithArray:[sourceString  componentsSeparatedByCharactersInSet:separatorSet]];
    [array removeObject:@""];

    bannerView.rootViewController = [array objectAtIndex:3];
    [bannerView loadRequest:[[AdManager sharedInstance] adMobrequest]];

    return bannerView;
}

-(GADBannerView*)adMobBannerWithAdUnitID:(NSString*)adUnitID andOrigin:(CGPoint)origin
{

    GADBannerView *bannerView = [[GADBannerView alloc] initWithAdSize:GADAdSizeFromCGSize(CGSizeMake([UIScreen mainScreen].bounds.size.width, AdmobBannerHeight)) origin:origin];
    bannerView.adUnitID = adUnitID;
    NSString *sourceString = [[NSThread callStackSymbols] objectAtIndex:1];
    // Example: 1   UIKit                               0x00540c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
    NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"];
    NSMutableArray *array = [NSMutableArray arrayWithArray:[sourceString  componentsSeparatedByCharactersInSet:separatorSet]];
    [array removeObject:@""];

    bannerView.rootViewController = [array objectAtIndex:3];
    [bannerView loadRequest:[[AdManager sharedInstance] adMobrequest]];

    return bannerView;
}

哪个好,但是,我需要停止出现在某个特定点(例如购买)的视图,我购买时目前有一个NSNotification设置,以隐藏横幅,我似乎无法隐藏它!

我想这是设置视图从superview中删除的情况,但似乎无法找到正确的方法。

1 个答案:

答案 0 :(得分:1)

在添加视图之前存储对视图的引用。

self.adBanner = [[AdManager sharedInstance] adMobBanner];
[self.view addSubview:self.adBanner];

然后你可以使用:

[self.adBanner removeFromSuperView];

除非我遗漏了你应该做的所有事情。