简体   繁体   中英

Admob in ToolBar UITableViewController Xcode

I am developing an application in Xcode using objective-c. My problem is that I am trying to show admob advertising in my UITableViewController using a ToolBar but this bar is not showing anything. I have found a lot of responses on the internet about how to put this ToolBar in the UITableViewController, but for my case is not showing anything. I know that I have a low level of Xcode but I am trying to improve everyday.

This is my piece of code for the admob:

[self.navigationController setToolbarHidden:NO];
    self.bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
    self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716"; //TEST!!!!!
    self.bannerView.rootViewController = self;
    [self.navigationController.toolbar addSubview:self.bannerView];

    GADRequest *request = [GADRequest request];
    request.testDevices = @[ @"",];
    [self.bannerView loadRequest:request];

This is my MainTableViewController.h:

#import <UIKit/UIKit.h>
#import "Restaurant.h"

@import GoogleMobileAds;
@interface MainTableViewController : UITableViewController
@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButton;
@property (nonatomic, strong) NSArray<Restaurant *> *originData;
@property (nonatomic, strong) NSMutableArray<Restaurant *> *filteredRest;
@property (nonatomic, assign) Boolean isFiltered;
@property (weak, nonatomic) IBOutlet UISearchBar *mySearchBar;
@property (strong, nonatomic) IBOutlet UITableView *RestTableView;
@property (weak, nonatomic) IBOutlet UIToolbar *admobToolBar;
@property(weak, nonatomic) IBOutlet GADBannerView *bannerView;

@end

And this is my MainTableviewController.m:

    #import "MainTableViewController.h"
    #import "SWRevealViewController.h"
    #import "RestTableViewCell.h"
    #import "RestViewController.h"
    #import "Restaurant.h"

    @interface MainTableViewController ()
    @end
    @implementation MainTableViewController

    @synthesize mySearchBar, filteredRest, isFiltered, originData;

    - (void)viewDidLoad {
        [super viewDidLoad];

        _barButton.target = self.revealViewController;
        _barButton.action = @selector(revealToggle:);

        [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

        self.RestTableView.tableFooterView = [[UIView alloc] init]; /*Esta linea hace que en la tabla solo aparezcan el numero de filas que tienes establecidas, es decir, que las vacias no aparezcan*/



        [self.navigationController setToolbarHidden:NO];
        self.bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
        self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716"; //TEST!!!!!
        //self.bannerView.adUnitID = @"ca-app-pub-6926442062866079/7221537141";
        self.bannerView.rootViewController = self;
        [self.navigationController.toolbar addSubview:self.bannerView]; 
        GADRequest *request = [GADRequest request];
        request.testDevices = @[ @"",];
        [self.bannerView loadRequest:request];

        originData = @[
            [[Restaurant alloc] init:@"80 Grados" descripiton:@"Malasaña" image:@"80_grados.jpg"],
            [[Restaurant alloc] init:@"90 Grados" descripiton:@"Retiro" image:@"90_grados.jpg"],
            [[Restaurant alloc] init:@"B&B Babel" descripiton:@"Barrio de Chueca" image:@"babel.jpg"],
            [[Restaurant alloc] init:@"Babelia" descripiton:@"Barrio de Salamanca" image:@"babelia.jpg"],
            [[Restaurant alloc] init:@"Bacira" descripiton:@"Chamberí" image:@"bacira.jpg"],
            [[Restaurant alloc] init:@"Bar Galleta" descripiton:@"Malasaña" image:@"bar_galleta.jpg"],
            [[Restaurant alloc] init:@"Bar Tomate" descripiton:@"Chamberí" image:@"bar_tomate.jpg"],

        filteredRest = [NSMutableArray new];
        isFiltered = NO;

    }
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    { 
        // Return the number of sections.
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        if (isFiltered == YES) {
            return filteredRest.count;
        } else {
            return originData.count;
        }
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"TableCell";
        RestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        // Configure the cell...
        if (isFiltered == YES) {
            cell.TitleLabel.text = [filteredRest objectAtIndex:indexPath.row].title;
            cell.DescriptionLabel.text = [filteredRest objectAtIndex:indexPath.row].desc;
            cell.RestImage.image = [UIImage imageNamed:[filteredRest objectAtIndex:indexPath.row].image];
        } else {
            cell.TitleLabel.text = [originData objectAtIndex:indexPath.row].title;
            cell.DescriptionLabel.text = [originData objectAtIndex:indexPath.row].desc;
            cell.RestImage.image = [UIImage imageNamed:[originData objectAtIndex:indexPath.row].image];
        }

        cell.RestImage.layer.cornerRadius = 6;
        cell.RestImage.clipsToBounds = YES;
        cell.RestImage.layer.borderWidth = 1;

        return cell;
    }


    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {

        if ([[segue identifier] isEqualToString:@"ShowDetails"]){
            RestViewController *restviewcontroller = [segue destinationViewController];

            NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];

            if (isFiltered) {
                restviewcontroller.DetailModal = filteredRest[myIndexPath.row];
            } else {
                restviewcontroller.DetailModal = originData[myIndexPath.row];
            }
        }
    }

@end

The ToolBar is not showing anything:

在此处输入图片说明

What can be wrong? I need your help! Than you very much for your responses!

Is there any reason you are using a UIToolBar to show this ad?

I would recommend instead of using a UITableViewController, use a UIViewController and make a property in there for a UITableView.

Once you have done that you can setup your constraints like so:

=====

top

tableview

ad view (set height)

bottom

======

The benefit of using a UIViewController with a table view in it is that you can put whatever else on the screen, not just the tableview :)

I hope that helps! Feel free to reply if you need more info on where to start with that!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM