BOOL在全球宣布,但某些功能无法看到它

时间:2015-08-12 07:06:45

标签: ios objective-c

好的,我在这里疯了。我正在使用Xcode 6.4,我也尝试了新的7 beta 3.

所发生的是我声明为全局变量的任何东西(例如BOOL)都不能被某些方法/函数看到。 -(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error可以看到没问题但是  @interface BannerViewController : UIViewController { BOOL isInternetActive; } 和其他一些人不可以。

我知道全局变量是危险的,但请让我知道我做错了什么。谢谢!

我的档案:

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{

isInternetActive = NO; //it doesn't compile because of this. Error is "Use of undeclared identifier 'isInternetActive'

}

m file:

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
//#import <GoogleMobileAds/GoogleMobileAds.h> //Google
@import GoogleMobileAds;


extern NSString * const BannerViewActionWillBegin;
extern NSString * const BannerViewActionDidFinish;

@interface TestBannerViewController : UIViewController
{

    GADBannerView *admobBannerView;
}

@property (nonatomic) BOOL isInternetActive;



- (instancetype)initWithContentViewController:(UIViewController *)contentController;

@end

编辑显示整个h和m文件: h文件:

#import "TestBannerViewController.h"
//#import <GoogleMobileAds/GoogleMobileAds.h> //Google
@import GoogleMobileAds;


NSString * const BannerViewActionWillBegin = @"BannerViewActionWillBegin";
NSString * const BannerViewActionDidFinish = @"BannerViewActionDidFinish";


@interface TestBannerViewController ()

// This method is used by BannerViewSingletonController to inform instances of    TestBannerViewController that the banner has loaded/unloaded.
- (void)updateLayout;

@end

@interface BannerViewManager : NSObject <ADBannerViewDelegate>

@property (nonatomic, readonly) ADBannerView *bannerView;

//@property (nonatomic, weak) GADBannerView *admobBannerView; //Google


+ (BannerViewManager *)sharedInstance;

- (void)addBannerViewController:(TestBannerViewController *)controller;
- (void)removeBannerViewController:(TestBannerViewController *)controller;

@end

@implementation TestBannerViewController {
    UIViewController *_contentController;

}

@synthesize isInternetActive;


- (instancetype)initWithContentViewController:(UIViewController *)contentController
{

NSAssert(contentController != nil, @"Attempting to initialize a   BannerViewController with a nil contentController.");

    self = [super init];
    if (self != nil) {
        _contentController = contentController;
        [[BannerViewManager sharedInstance] addBannerViewController:self];
    }
    return self;
}

- (void)dealloc
{
    [[BannerViewManager sharedInstance] removeBannerViewController:self];
}

- (void)loadView
{
    UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

   // Setup containment of the _contentController.
    [self addChildViewController:_contentController];
    [contentView addSubview:_contentController.view];
    [_contentController didMoveToParentViewController:self];

    NSLog(@"Google Mobile Ads SDK version: %@", [GADRequest sdkVersion]);

    self.view = contentView;


}

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return [_contentController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
#endif

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [_contentController preferredInterfaceOrientationForPresentation];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [_contentController supportedInterfaceOrientations];
}

- (void)viewDidLayoutSubviews
{

    CGRect contentFrame = self.view.bounds, bannerFrame = CGRectZero;
    ADBannerView *bannerView = [BannerViewManager sharedInstance].bannerView;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
    NSString *contentSizeIdentifier;

    if (contentFrame.size.width < contentFrame.size.height) {
        contentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    } else {
        contentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
    }
    bannerFrame.size = [ADBannerView sizeFromBannerContentSizeIdentifier:contentSizeIdentifier];
#else

    bannerFrame.size = [bannerView sizeThatFits:contentFrame.size];
#endif

    if (bannerView.bannerLoaded) {
        contentFrame.size.height -= bannerFrame.size.height;
        bannerFrame.origin.y = contentFrame.size.height;

    } else {

        //contentFrame.size.height -= bannerFrame.size.height;
        bannerFrame.origin.y = contentFrame.size.height;
    }
    _contentController.view.frame = contentFrame;

    if (self.isViewLoaded && (self.view.window != nil)) {
        [self.view addSubview:bannerView];
        bannerView.frame = bannerFrame;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
        bannerView.currentContentSizeIdentifier = contentSizeIdentifier;
#endif
    }
}

- (void)updateLayout
{
        [UIView animateWithDuration:0.25 animations:^{
        [self.view setNeedsLayout];
        [self.view layoutIfNeeded];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.view addSubview:[BannerViewManager sharedInstance].bannerView];
}

- (NSString *)title
{
    return _contentController.title;

}

@end

@implementation BannerViewManager {
     ADBannerView *_bannerView;
     NSMutableSet *_bannerViewControllers;
}

+ (BannerViewManager *)sharedInstance
{
    static BannerViewManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[BannerViewManager alloc] init];
    });
    return sharedInstance;
}

- (instancetype)init
{
    self = [super init];
    if (self != nil) {
    if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)])         {
        _bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
    } else {
        _bannerView = [[ADBannerView alloc] init];
    }
        _bannerView.delegate = self;
        _bannerViewControllers = [[NSMutableSet alloc] init];
    }
    return self;

}

- (void)addBannerViewController:(TestBannerViewController *)controller
{
    [_bannerViewControllers addObject:controller];
}

- (void)removeBannerViewController:(TestBannerViewController *)controller
{
    [_bannerViewControllers removeObject:controller];
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    for (TestBannerViewController *bvc in _bannerViewControllers) {
        [bvc updateLayout];
    }
}


-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    for (TestBannerViewController *bvc in _bannerViewControllers) {
        [bvc updateLayout];
    }

    isInternetActive = YES;

}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    [[NSNotificationCenter defaultCenter]   postNotificationName:BannerViewActionWillBegin object:self];
    return YES;

}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
    [[NSNotificationCenter defaultCenter] postNotificationName:BannerViewActionDidFinish object:self];
}


@end

m file:

struct DblWrapper{
  double value;
  void func1(); // maybe to print nicely
  void func2(); // maybe to register this for automatic capture at data source
}

2 个答案:

答案 0 :(得分:1)

您可以在项目中创建新的头文件。

你可以这样写:

static BOOL isInternetActive;

将此标题文件导入您要访问此bool值的位置。

enter image description here

enter image description here

希望这有帮助。

答案 1 :(得分:0)

我想我明白了。在m文件中有两个@implementation文件(两个类?),这就是为什么在h文件中声明全局时最后一个类无法看到它。