ios - 基于 USER_INTERFACE_IDIOM 运行 Objective C

标签 ios uitableview admob

我使用以下代码在 Header 中显示 Admob 横幅 TableView 。横幅应仅显示在 iPhone 上,而不是 iPad 上。这段代码在 iPhone 上运行,但在 iPad 上崩溃了。我哪里出错了?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        GADBannerView *sampleView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];

        sampleView.adUnitID = @"ca-app-pub-mycode";

        sampleView.rootViewController = self;

        [sampleView loadRequest:[GADRequest request]];

        return sampleView;
    }
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
    }
}

最佳答案

您必须从该方法返回一个 UIView(或子类,或 nil)。在 iPhone 上返回 sampleView,但在 iPad 上运行此方法时不会返回任何内容。

如果您不想在 iPad 上返回 View ,请执行以下操作:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        GADBannerView *sampleView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];

        sampleView.adUnitID = @"ca-app-pub-mycode";

        sampleView.rootViewController = self;

        [sampleView loadRequest:[GADRequest request]];

        return sampleView;
    } else {
        return nil;
    }
}

关于ios - 基于 USER_INTERFACE_IDIOM 运行 Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36661309/

相关文章:

iOS AdMob 何时会调用 `didFailToPresentFullScreenContentWithError`

ios - 全宽 Webview iOS 问题

ios - 在 iOS 应用程序周围传递用户信息的最佳方式?

ios - 为什么 GADBannerView 未声明?

ios - 选择表中的行导致崩溃

ios - 更新 TableView 上的所有单元格而不使操作太 "heavy"内存

ios - View Controller 加载时的插页式广告

ios - 将视频保存到 DocumentsDirectory

ios - 如何从 CAShapeLayer 获取坐标

swift - UITableView 删除了在 canEditRowAt 中不可编辑的行上的披露指示符和选择