How do I initialize Admob and set the subview - iphone

I'm fairly new to xcode, but i'm having some trouble adding admob to my app.
I followed the instructions for Admob but the ads are not showing up.
Im thinking i need to add the new view i created AdViewController to appsdelegate. Is there a code i should add for that?
BTW: Im using a tabbar controller as the rootviewcontroller

In the view controller in which you want to display the add , do the following :
.h
#interface MyViewController: UIViewController
{
GADBannerView *adBanner;
}
#end
.m
#implementation MyViewController
// In the viewDidLoad method of the controller create a adbanner
-(void) viewDidLoad
{
[super viewDidLoad];
adBanner_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0, 0, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)];
adBanner_.delegate = self;
adBanner_.rootViewController = self;
adBanner_.adUnitID = the_ad_ID_given_by_Google;
GADRequest *request = [GADRequest request];
[adBanner loadRequest:request];
[self.view addSubview:adBanner];
}
#end

You need to set the rootViewController to be the UITabBarController, but you need to add the GADBannerView object into the view for the view Controller that's currently being shown.
There's an example using a custom UITabBar solution here.

Related

Add Tapku calendar to app

I am trying to add the Tapku calendar to my app. I am using storyboards, I have added the Tapku library, imported the necessary files and add the TKCalendarMonthViewDelegate methods. I am adding the calendar to a UIView called calendarView. When I run the app the calendar doesn't appear, just the view with nothing inside it.
-(void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:NO animated:YES];
self.navigationItem.hidesBackButton = YES;
calendar = [[TKCalendarMonthView alloc] init];
calendar.delegate = self;
calendar.dataSource = self;
calendar.frame = CGRectMake(0, 0, calendar.frame.size.width, calendar.frame.size.height);
// Ensure this is the last "addSubview" because the calendar must be the top most view layer
[self.view addSubview:calendar];
[calendar reload];
// Do any additional setup after loading the view.
}
Can anyone help me please?
try by specifing frame points directly,like this
calendar.frame = CGRectMake(0, 0, 320,400);
If you're adding TKCalendarMonthView to your view controller using a Storyboard, then you should not also be initializing another instance of TKCalendarMonthView in your view controller's -viewDidLoad method.
In your Storyboard:
Add a TKCalendarMonthView to your view controller.
Set the size contraints.
Connect TKCalendarMonthView to the outlet (see below) in your view controller.
In your view controller:
Add an outlet for the TKCalendarMonthView.
#interface YourViewController () <TKCalendarMonthViewDataSource, TKCalendarMonthViewDelegate>
#property (weak, nonatomic) IBOutlet TKCalendarMonthView *calendarMonthView;
#end
In -viewDidLoad, connect TKCalendarMonthView's delegate and data source. Note, you can also do this in the Storyboard if you first add the IBOutlet annotate to the delegate and dataSource properties in TKCalendarMonthView.h
#implementation YourViewController
...
- (void)viewDidLoad
{
[super viewDidLoad];
...
self.calendarMonthView.delegate = self;
self.calendarMonthView.dataSource = self;
However these changes alone will not get the TKCalendarMonthView to display the calendar. The reason is that the view is getting initialized by the Storyboard but none of the existing -init methods are called when loaded by the Storyboard. So you will need to add an -initWithCoder: method to TKCalendarMonthView.m. The following example will call the default -init: method.
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [self init];
if (self) {
}
return self;
}
If you do all this, you should see the rendered calendar instead of a blank view.

page-based app with uitableview; how to show detail view and show another data in the table when goes to next page?

I have page-based app. On each page I have 3 uibuttons at the top, uiscrollview with alphabet (uibuttons to sort data in uitable) at the right and uitableview at the center. How to show cell's detail view? If it is necessary to add uinavigationcontroller I can't do this. If I adds it, it disables interaction with my table, buttons and scrollview.
And another question is how to show new data in tableview and scrollview when goes to next page??
I have rootViewController class and DataViewController class.
rootViewController listing:
#interface RootViewController ()
#property (readonly, strong, nonatomic) ModelController *modelController;
#end
#implementation RootViewController
#synthesize pageViewController = _pageViewController;
#synthesize modelController = _modelController;
#synthesize navContr = _navContr;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Configure the page view controller and add it as a child view controller.
//[self presentModalViewController:navContr animated:YES];
self.pageViewController = [[[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil] autorelease];
self.pageViewController.delegate = self;
DataViewController *startingViewController = [self.modelController viewControllerAtIndex:0 storyboard:self.storyboard];
NSArray *viewControllers = [NSArray arrayWithObject:startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
self.pageViewController.dataSource = self.modelController;
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
self.navContr = [[UINavigationController alloc] initWithRootViewController:self.pageViewController];
[self.view addSubview:self.navContr.view];
// Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.
CGRect pageViewRect = self.view.bounds;
self.pageViewController.view.frame = pageViewRect;
[self.pageViewController didMoveToParentViewController:self];
// Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
for (UIGestureRecognizer *recognizer in self.pageViewController.gestureRecognizers){
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]){
[recognizer setEnabled:NO];
}
}
}
After few manipulations it works but I need help to get it work fine!
So now it looks like this
Next question: how do I remove the brown space at the top???
::UPDATE::
Problem solved. It needs just to set y-axis position of UINavigationController to -20 ;)
i'm not sure if this link about creating navigation-based project may help you.. (http://iosmadesimple.blogspot.com/2012/08/navigation-based-project-doing-it.html)
From that tutorial, there's a class called SampleViewController, subclass of UIViewController. You might want to put a tableView in SampleViewController.xib file. Then in your SampleViewController.h file, add an IBOutlet UITableView* yourTable property and synthesize it. Connect it to your tableView in your .xib file. //Or you may do it programmatically
in your SampleViewController.h, make your interface header look like this.. I think you already know this...
#interface SampleViewController:UIViewController < UITableviewDelegate, UITableViewDatasource >
in your SampleViewcontroller.m, under viewDidLoad method, set the table delegate and datasource to self:
yourTableView.delegate = self;
yourTableView.datasource = self;
Afterwhich, you implement tableView delegate and datasource methods... //you already know those since you were already able to show a tableview ;)
one of these methods is the "tableview:didSelectAtIndexpath:" --> this is the part wherein you can put your code when you click one of the cells.
Let's assume you have the DetailsViewController Class, this is the class you would like to show after clicking a cell and show its details.
DetailsViewController Class must have a variable that will accept the data you would like to show. Let's say, an NSString *detailsMessage; //do the #property and #synthesize thing...
Let's go back to SampleViewController.m file, under tableview:didSelectAtIndexpath: Method:
inside that Method.. put these codes.
DetailsViewController *detailsVC = [[DetailsViewController alloc] init];
detailsVC.detailsMessage = #"The Data you want to pass.";
[self.navigationController pushViewController:detailsVC animated:YES];
I hope this helps. :(
There may be other ways but by far the easiest way is to use a navigation controller. In fact, it is built to do exactly this.
If you don't want the navigationBar then you can hide it in the viewWillAppear function.
[self.navigationController setNavigationBarHidden:YES animated:YES];
Then you can add an other UIViewController to push when the user selects a cell.
Having read your OP again I'm not sure how you are adding your navigationController.
To use a navigationController you create it and load it at start time. You then create your current viewController (the one with the buttons and table etc...) and set this as the rootViewController of the navigationController.
Then you display the navigationController.
Could you explain how you are adding your navigationController as it may help understand what is going wrong.
Thanks
::EDIT::
OK, my assumption was correct.
The way you are using the navigation controller is not how it was intended.
OK, so at the moment your AppDelegate file will have a method Application didFinishLaunching...
It will look something like this...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[OJFViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
You should change it so that it is like this...
first add a property to your appDelegate...
#property (nonatomic, strong) UINavigationController *navigationController;
Then change the didFinishLaunchingMethod to this...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[OJFViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
This will still show you MainViewController but it will now be contained within a navigationController.
Next in your MainViewController function viewWillAppearAnimated add the line...
[self.navigationController setNavigationBarHidden:YES animated:animated];
This will then hide the navigationBar at the top of the view so you still have access to your buttons.
The you need a new ViewController and xib file (for example DetailViewController).
When the user selects a table row you need to then do something like...
DetailViewController *detailView = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
//pass in details of which row was selected.
[self.navigationController pushViewController:detailView animated:YES];
This will then display your new view and new viewController. You will also need to write a way of passing the data in (set up a property on the DetailViewController).
Hope this helps.

Dynamiclly creating View in iOS

I have a project with one View. I'm drawing everything on it programically. So when i want to add another View (screen) to my project i create a class inherits from the UIViewController class and implements method
- (void)viewDidLoad
And then i want to load this View from my original View, and i do this:
In ViewController.h
#import <UIKit/UIKit.h>
#import "TestViewControllerClass.h"
#interface ViewController : UIViewController <UITableViewDataSource> {
}
#property (strong,nonatomic) TestViewControllerClass *testView;
#end
In ViewController.m
#implementation ViewController
#synthesize testView;
- (void)viewDidLoad
{
[super viewDidLoad];
testView = [[TestViewControllerClass alloc] init];
[self.view addSubview:testView]; //crash here
}
And then in my TestViewControllerClass.h
#import <UIKit/UIKit.h>
#interface TestViewControllerClass : UIViewController
#end
And TestViewControllerClass.m
- (void)viewDidLoad
{
[super viewDidLoad];
}
To check if method wiewDidLoad will be executed i put there a breakpoint, but nothing happend. In fact, my app crash (I put comment at code where).
When crashes i receive: -[TestViewControllerClass superview]: unrecognized selector sent to instance 0x683aca0
Replace
[self.view addSubview:testView]; //crash here
with
[self.view addSubview:testView.view];
Use this code...
[self.view addSubview:testView.view];
Hope,this will help you...
What you're doing is settings your ViewController as the view, not the real view
testView = [[TestViewControllerClass alloc] init];
[self.view addSubview:testView]; //crash here
this will obviously crash. Assuming you have a view variable declared in your header file called view, use
testView = [[TestViewControllerClass alloc] init];
[self.view addSubview:testView.view];
You can load two types for uiviewcontroller loading below:
[self.view addSubview:testView.view];
(or)
use presentmodalviewcontroller below code:
testView *popupView = [[testView alloc] initWithNibName:#"testView" bundle:nil];
popupView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:popupView animated:YES];
dimiss modal view cntroller below code:
[self dismissModalViewControllerAnimated:YES];
thanks..!

Modal UIViewController doesn't appear

I'm attempting to load a Modal View Controller (1st) from a Modal View Controller (2nd). While it sounds complicated, it probably isn't.
The 1st controller is actually a UIWebView which is initialized in the loadView method of the .m file:
- (void)loadView {
// Initialize webview and add as a subview to LandscapeController's view
myWebView = [[[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
//CGRect forceframe = CGRectMake(0, 0, 480, 320);
//myWebView = [[[UIWebView alloc] initWithFrame:forceframe] autorelease];
myWebView.scalesPageToFit = YES;
myWebView.autoresizesSubviews = YES;
myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
myWebView.delegate = self;
self.view = myWebView;
}
Then in viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
// Load HTML file as an NSURL request
[self.myWebView loadHTMLString:updated_html baseURL:nil];
// Invoke the covering modal view on condition
if (some_condition) {
landscapeCoverController = [[UIViewController alloc] initWithNibName:#"LandscapeCoverController" bundle:[NSBundle mainBundle]];
[self presentModalViewController:landscapeCoverController animated:YES];
[landscapeCoverController release];
}
The intended 2nd Modal View Controller (landscapeCoverController) is initialized with a NIB that I set up in IB.
My intended objective, is to conditionally cover up the UIWebView with the "LandscapeCoverController" view, which will have some buttons and interactivity which will result in the 2nd Modal View being dismissed.
Why isn't my landscapeCoverController loading? Any thoughts greatly appreciated!
Also...the 1st Modal View controller (LandscapeViewController) .h looks like:
#class LandscapeCoverController;
#interface LandscapeViewController : UIViewController <UIWebViewDelegate> {
UIWebView *myWebView;
LandscapeViewController *landscapeCoverController;
}
#property (nonatomic, retain) UIWebView *myWebView;
#property (nonatomic, retain) LandscapeViewController *landscapeCoverController; // Modal view controller
and...the 2nd Modal View controller (landscapeCoverController) viewDidLoad does nothing:
// NIB initialized in LandscapeViewController.m viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
}
as I think the
landscapeCoverController = [[UIViewController alloc] initWithNibName:#"LandscapeCoverController" bundle:[NSBundle mainBundle]];
[self presentModalViewController:landscapeCoverController animated:YES];
[landscapeCoverController release];
statement should handle initialization and loading of the controller...
You are declaring landscapeCoverController as an instance of LandscapeViewController, and allocating it as a UIViewController. This is most likely your problem (probably the first one, as you aren't calling any methods specific to LandscapeViewController). Also, since landscapeCoverController is an instance variable, you don't really need to release it after presentModalViewController. Try to pick more dissimilar class names. It will save you from confusion like this in the future.

Hide tab bar control in tabbar project

I am working on a tabbar project and in this i also have navigation controller. and i am doing below steps :-
Show the main screen
navigation from first tab to 5 next screens.
and on the 6th screen i want to show the tabbarcontroller and want to show my other tab bar.
i tried the below code :-
self.navigationController.tabBarController.hidesBottomBarWhenPushed = YES;
and some others. but did not get any success yet. so can any one suggest how to i do this?
Thanks
Create two files .h and .m without .xib
//.h file
#import <UIKit/UIKit.h>
#class Class1, Class2;
#interface TabbarController : UITabBarController
{
Class1 *class1;
Class2 *class2;
UINavigationController *nav1,*nav2;
}
#end
//.m file
#implementation TabbarController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
class1 =[[Class1 alloc] initWithNibName:#"Class1" bundle:nil];
nav1=[[UINavigationController alloc] initWithRootViewController:class1];
class1.title = #"class1";
class1.tabBarItem.image = [UIImage imageNamed:#"tab1.png"];
class1.navigationController.navigationBar.hidden = TRUE;
class2 =[[Class2 alloc] initWithNibName:#"Class2" bundle:nil];
nav2=[[UINavigationController alloc] initWithRootViewController:class2];
class2.tabBarItem.image = [UIImage imageNamed:#"tab2.png"];
class2.title = #"class2";
class2.navigationController.navigationBar.hidden = TRUE;
NSArray *controllers = [NSArray arrayWithObjects:nav1,nav2,nil];
self.viewControllers = controllers;
}
Redirect your view to this view wherever you need tabbar.
TRy it by this:
Create the Object of Delegate class
#import "DelegateClass.h"
DelegateClass *appDel;
Now in .m class
-(void)viewDidLoad
{
[super viewDidLoad];
appDel= (DelegateClass *)[[UIApplication sharedApplication]delegate];
}
Now just do like this in the View from where you are navigation,
appDel.tabBarController.hidesBottomBarWhenPushed = YES;
This was just a tricky part.It worked greatly for me :)
You have to use custom UItabBarController.
see creating custom TabBar Controller