我使用以下代码在 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/