Declaring custom UINavigationController in xcode 4.2 - iphone

I was wondering how can declare a custom UINavigationController in Xcode 4.2 ? I have created a project which uses an API and needs UINavigationController my project does not use story boards and is viewBased application. thanks

I wrote my own. The key is to subclass UIViewController AND remember to set self.title and the icon to its first "contained" class's otherwise nothing will show up on the tabBarIcons. UINavigationController is only one level deep from UIViewController, so you can view the header and easily see what it implements, but those were the only real keys to "copy over".
In Interface Builder, assuming you have a nib to go along with it, make a main view that's the size of the screen (311 if you have a tabBar and Status Bar), then create a top view that's IB-outletted to be the navigation bar, and a lower view that's outletted as the container. Then do something like this:
note: I messed with the center point as I ran into many issues regarding trying to move the views without having them offset by the pixel height, even though I'm aware of relative positioning of subviews, it just didnt' work for some reason, even if only moving sideways
I'm posting this code because nobody seems to have this type of thing up. This might help someone, or more.
Stephen Johnson.
import "CustomNavigationController.h"
#implementation CustomNavigationController
#synthesize backgroundImg, title1, title2, title3;
- (id) initWithRootViewController:(UIViewController*)c;
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
containedControllers = [[NSMutableArray alloc] initWithObjects:c, nil];
self.title1.text = c.title; //a custom outlet for text of title, resembling the NavigationController's title basically
[container addSubview:c.view];
c.view.frame = container.bounds;
back.hidden = YES; //backbutton
c.customNavigationController = self;
self.title = c.title;
self.tabBarItem.image = c.tabBarItem.image;
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void) dealloc;
{
[containedControllers removeAllObjects];
[containedControllers release];
[super dealloc];
}
- (void) pushViewController:(UIViewController*)v animated:(BOOL)a;
{
float w = container.frame.size.width;
float h = container.frame.size.height;
[containedControllers addObject:v];
[self.view addSubview:v.view];
// v.view.frame = CGRectMake(w,0,w,h);
v.view.frame = container.bounds;
v.view.center = CGPointMake(v.view.center.x + w, v.view.center.y + container.frame.origin.y);
v.customNavigationController = self;
float time = a ? 0.31 : 0;
UIViewController * lastViewController = nil;
lastViewController = (UIViewController*)[containedControllers lastObject];
[UIView animateWithDuration:time animations:^{
for (UIViewController * c in containedControllers) {
// c.view.frame = CGRectMake(c.view.frame.origin.x + w*direction, 0, w, h);
c.view.center = CGPointMake(c.view.center.x + w*-1, c.view.center.y);
}
} completion:^(BOOL finished) {
self.title1.text = v.title;
back.hidden = NO;
}];
}
- (void) popViewControllerAnimated:(BOOL)a;
{
float w = container.frame.size.width;
float h = container.frame.size.height;
float time = a ? 0.31 : 0;
float direction = 1;
[UIView animateWithDuration:time animations:^{
for (UIViewController * c in containedControllers) {
// c.view.frame = CGRectMake(c.view.frame.origin.x + w*direction, 0, w, h);
c.view.center = CGPointMake(c.view.center.x + w*direction, c.view.center.y);
}
} completion:^(BOOL finished) {
// lastViewController = (UIViewController*)[containedControllers lastObject];
[containedControllers removeLastObject];
self.title1.text = ((UIViewController*)[containedControllers lastObject]).title;
if ([containedControllers count] > 1) {
back.hidden = NO;
}
else
back.hidden = YES;
}];
}
- (IBAction) popLastVC;
{
[self popViewControllerAnimated:YES];
}
#end

It's quite simple to subclass a UINavigationController through inheritance. It's a key concept of OOP.
//YourClass.h
#interface YourClass : UINavigationController
#end
//YourClass.m
#implementation YourClass
#end
But
This class is generally used as-is but may be subclassed in iOS 6 and later.
as written in the Overview of UINavigationController. So you may not be able to subclass a UINavigationController if you are supporting iOS 5 or earlier. Maybe your subclass could not work correctly. You can find a good discussion on this stackoverflow topic.

Related

UIImagePickerControllerCameraDeviceFront works every other time

This question is very similar to an existing question asked here UIImagePickerControllerCameraDeviceFront only works every other time I tried the solution presented but it didn't work for me
I have a simplest of a project with two view controllers. In the blue one I am displaying a small UIView with a UIImagePickerController in it. NOTE: I am displaying front facing camera when app is launched.
I hit the next button and go to orange view controller and when I hit the back button and come back to blue view controller the UIImagePickerController flips from Front to rear. I guess the reason is that it thinks its busy and moves to the rear cam. If I keep moving back and forth between the view controllers the camera keeps flipping front, back, front, back, front, back...
Here is my code and screenshots, what am I doing wrong?
In my *.h
#import <UIKit/UIKit.h>
#interface v1ViewController : UIViewController <UIImagePickerControllerDelegate>
{
UIImagePickerController *picpicker;
UIView *controllerView;
}
#property (nonatomic, retain) UIImagePickerController *picpicker;
#property (nonatomic, retain) UIView *controllerView;
#end
In my *.m file (This code is only used when blue colored view controller is displayed)
#import "v1ViewController.h"
#import <MobileCoreServices/UTCoreTypes.h>
#implementation v1ViewController
#synthesize picpicker;
#synthesize controllerView;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
picpicker = [[UIImagePickerController alloc] init];
picpicker.delegate = self;
picpicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
picpicker.sourceType = UIImagePickerControllerSourceTypeCamera;
picpicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picpicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picpicker.showsCameraControls = NO;
picpicker.navigationBarHidden = NO;
picpicker.wantsFullScreenLayout = NO;
controllerView = picpicker.view;
[controllerView setFrame:CGRectMake(35, 31, 250, 250)];
controllerView.alpha = 0.0;
controllerView.transform = CGAffineTransformMakeScale(1.0, 1.0);
[self.view addSubview:controllerView];
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
controllerView.alpha = 1.0;
}
completion:nil
];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[picpicker dismissModalViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[picpicker dismissModalViewControllerAnimated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
You are dismissing the controller in both the viewDidDisappear and viewWillDisappear methods.
That could be the cause of your problem.
Although I do not have a device with a camera available right now to verify this, it seems that you're not dismissing the pickerview controller correctly. The documentation states that you should call dismissModalViewControllerAnimated: on the parent controller in order to dismiss the picker (though, calls to presented controllers will propagate to presenters - so this is not the problem), but in your case you're not displaying the controller modally in the first place so it will not work.
What I would try in this case is to release the picker instead (if not under ARC) and set it to nil (instead of calling [picpicker dismissModalViewControllerAnimated:YES];).
PS. In fact, it seems that there is a bigger problem with your design. Since each button is set to present the other party modally you are not dismissing any of the controllers ever. The controllers just keep stacking on each other. You should either consider to embed them in a navigation controller and have it handle the hierarchy or just set dismissModalViewControllerAnimated: (dismissViewControllerAnimated:completion: on iOS5+) as the action of the second controller's button instead of a modal segue.
This is a very simple issue. I don't know why this happens exactly, but it seems that UIImagePickerController was designed to recreated each time it's needed instead of keeping any reference to it, which seems logical if you think about it. Basically, you need to recreate and reconfigure your picker each time. Below I've pasted some code to give an image of what I mean.
Simple solution:
- (UIImagePickerController *)loadImagePicker {
UIImagePickerController *picpicker = [[UIImagePickerController alloc] init];
picpicker.delegate = self;
picpicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
picpicker.sourceType = UIImagePickerControllerSourceTypeCamera;
picpicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picpicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picpicker.showsCameraControls = NO;
picpicker.navigationBarHidden = NO;
picpicker.wantsFullScreenLayout = NO;
return picpicker;
}
and in:
-(void)viewWillAppear:(BOOL)animated{
if(!self.picpicker){
self.picpicker = [self loadImagePicker];
[self.view addSubview: self.picpicker];
}
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.picpicker removeFromSuperview];
self.picpicker = nil;
}

is it a good practice to delete the AdBannerView on viewWillDisappear and add it back on viewWillAppear?

I am currently doing the following in my code avoid the issue of "obscured" ad. But is it a good practice? One potential problem is that - assume before the viewWillDisappear, there was an ad request send out, and then when the ad come back the adBannerView instance has gone. Would that be a big problem? Should I only do hideAdBanner instead?
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear: animated];
// create the ad banner view
[self createAdBannerView];
if (adBannerView != nil) {
UIInterfaceOrientation orientation = self.interfaceOrientation;
[self changeBannerOrientation:orientation];
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// iAd
if (adBannerView != nil) {
[self hideAdBanner];
adBannerView.delegate = nil;
[adBannerView release];
adBannerView = nil;
}
}
I use a singleton for an ad banner and call it into view on each ViewDidLoad. This automatically removes it from the previous view.
This is for adWhirl, but you should be able to adopt it for just iAD.
adWhirlSingleton.h
#import <Foundation/Foundation.h>
#import "AdWhirlDelegateProtocol.h"
#import "AdWhirlView.h"
#interface adWhirlSingleton : NSObject <AdWhirlDelegate> {
AdWhirlView *awView;
UIViewController *displayVC;
}
#property (strong, nonatomic) AdWhirlView *awView;
#property (strong, nonatomic) UIViewController *displayVC;
+(id)sharedAdSingleton;
-(void)adjustAdSize:(CGFloat)x:(CGFloat)y;
#end
adWhirlSingleton.m
#import "adWhirlSingleton.h"
#implementation adWhirlSingleton
static adWhirlSingleton* _sharedAdSingleton = nil;
#synthesize awView, displayVC;
+(id)sharedAdSingleton
{
#synchronized(self)
{
if(!_sharedAdSingleton)
_sharedAdSingleton = [[self alloc] init];
return _sharedAdSingleton;
}
return nil;
}
+(id)alloc
{
#synchronized([adWhirlSingleton class])
{
NSAssert(_sharedAdSingleton == nil, #"Attempted to allocate a second instance of a singleton.");
_sharedAdSingleton = [super alloc];
return _sharedAdSingleton;
}
return nil;
}
-(id)init
{
self = [super init];
if (self != nil) {
// initialize stuff here
self.awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
}
return self;
}
-(void)dealloc
{
displayVC = nil;
if (awView) {
[awView removeFromSuperview]; //Remove ad view from superview
[awView replaceBannerViewWith:nil];
[awView ignoreNewAdRequests]; // Tell adwhirl to stop requesting ads
[awView setDelegate:nil];
awView = nil;
}
}
-(void)adjustAdSize:(CGFloat)x :(CGFloat)y
{
[UIView beginAnimations:#"AdResize" context:nil];
[UIView setAnimationDuration:0.7];
awView.frame = CGRectMake(x, y, kAdWhirlViewWidth, kAdWhirlViewHeight);
[UIView commitAnimations];
NSLog(#"Recent Network Name: %#",[awView mostRecentNetworkName]);
}
-(BOOL)adWhirlTestMode
{
return YES;
}
-(NSString *)adWhirlApplicationKey
{
return #"xxxxxxxxxxxxx";
}
-(UIViewController *)viewControllerForPresentingModalView
{
return displayVC;
}
-(void)adWhirlDidReceiveAd:(AdWhirlView *)adWhirlView
{
NSLog(#"%s",__FUNCTION__);
NSLog(#"Recent Network Name: %#",[awView mostRecentNetworkName]);
//[self adjustAdSize];
}
-(void)adWhirlDidFailToReceiveAd:(AdWhirlView *)adWhirlView usingBackup:(BOOL)yesOrNo
{
NSLog(#"%s",__FUNCTION__);
}
#end
Then import adWhirlSingleton into each ViewController and in each viewWillAppear i just implement this:
adWhirlSingleton *adWhirlSingle = [adWhirlSingleton sharedAdSingleton];
adWhirlSingle.displayVC = self;
[adWhirlSingle adjustAdSize:0 :self.view.frame.size.height -50];
[self.view addSubview:adWhirlSingle.awView];
[self.view bringSubviewToFront:adWhirlSingle.awView];
NSLog(#"Ad Banner View");
but the view I have with a UITableView, I use this:
adWhirlSingleton *adWhirlSingle = [adWhirlSingleton sharedAdSingleton];
adWhirlSingle.displayVC = self;
[adWhirlSingle adjustAdSize:0 :self.tabBarController.view.frame.size.height -99];
[self.tabBarController.view addSubview:adWhirlSingle.awView];
NSLog(#"Should have added Ad!");
Hope that helps you a bit
Out of interest, why is that you are removing the ADBannerView?
Apple state that you should share the ADBannerView instances across views.
From the docs: "If your application has multiple tabs or views displaying an iAd banner, be sure to share a single instance of ADBannerView across each view."
i.e. Apple think you should have the ADBannerView presented at the top/front of your view hierarchy and just move it off-screen when there is no ad to show.
So, to answer the question, "is it bad practice to remove and then add it again?"
yes, Apple would say it is.

back button at root controller of uinavigationcontroller

I have a UINavigationController application with a root view controller and each time I am pushing a view controller to the stack. Let's say the stack is A B C D where A is the root view controller here. The issue is that when I am at view controller D and do a popToRootViewController it went back to A but A has a back button on it. When I click on back the back just slides in and disappear, why is this happening?
EDIT: I am actually subclassing my UINavigationController so that I can set my rootViewController as follows:
#import "CustomNavigationController.h"
#implementation CustomNavigationController
#synthesize fakeRootViewController;
//override to remove fake root controller
-(NSArray *)viewControllers {
NSArray *viewControllers = [super viewControllers];
if (viewControllers != nil && viewControllers.count > 0) {
NSMutableArray *array = [NSMutableArray arrayWithArray:viewControllers];
[array removeObjectAtIndex:0];
return array; }
return viewControllers; }
//override so it pops to the perceived root
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated {
//we use index 0 because we overrided “viewControllers”
((UIViewController *)[self.viewControllers objectAtIndex:0]).navigationItem.hidesBackButton = YES;
return [self popToViewController:[self.viewControllers objectAtIndex:0] animated:animated]; }
//this is the new method that lets you set the perceived root, the previous one will be popped (released)
-(void)setRootViewController:(UIViewController *)rootViewController {
rootViewController.navigationItem.hidesBackButton = YES;
[self popToViewController:fakeRootViewController animated:NO];
[self pushViewController:rootViewController animated:NO]; }
- (void)dealloc {
self.fakeRootViewController = nil;
[super dealloc]; }
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self){
UIViewController *fakeController = [[[UIViewController alloc] init] autorelease];
self.fakeRootViewController = fakeController;
NSMutableArray *array = [NSMutableArray arrayWithArray:[super viewControllers]];
[array insertObject:fakeController atIndex:0];
self.viewControllers = array;
}
return self; }
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use. }
#pragma mark - View lifecycle
/* // Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView { }
*/
/* // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad]; }
*/
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil; }
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait); }
#end
MORE UPDATE:
So after I set my rootViewController and then push a viewController and then tried to call popToRootViewController from that view, it all worked fine. However, if I push another viewController after the second one and then call the popToRootViewController, now I can see that weird back button on the root.
I too face the same problem. So in your root controller assign leftBarButtonItem equal to nil.
self.navigationItem.leftBarButtonItem = nil;
If your root controller reusing in the program. Then you need to check ->
BOOL needBackBarButton = (1 < [self.navigationController.viewControllers count]) ? YES : NO ;
if (! needBackBarButton)
{
self.navigationItem.leftBarButtonItem = nil;
}
For other controllers
if (needBackBarButton)
{
// Create custom navigationItem here.
}
else
{
self.navigationItem.leftBarButtonItem = nil;
}

AdBannerView shared across multiple views, including the rootviewcontroller, how?

No answers yet, but i've got an update that seems to have fixed the problem, any ideas why it works now though?!
I've got it to work in the way I intended by doing the following to every class that needs to display an adBanner:
1. In the layoutForCurrentOrientation method I added the following:
adBanner.delegate = self;
[self.view addSubview:adBanner];
2. In the deAlloc method on each class, I removed the following:
[adBanner removeFromSuperview];
Original question:
I'm attempting to use the iAdSuite sample code from Apple to use a single adBanner instance shared across all my views.
The sample implementation is designed to show the adbanner on each view that is called by the rootViewController, however, I would like my app to have ads on the rootViewController view also.
In my amended code:-
When I fire up the app, no banner is shown on the rootView, even though a method is called to request an ad banner. The class is set as the delegate for the ad and the delegate methods are available. These are called and the log for (adBanner.bannerLoaded) is NO.
As it is a shared object, if I switch views to from the rootView the ad is displayed in the other view.
When I return back to the rootView, the delegate method log shows that a banner is loaded, and it is positioned in a visible portion of the view. But the banner isn't visible.
In summary, i'm using the iAdSuite sample code for the AdBannerNavigation project, and trying to use it so that ad banners show on all views, including the rootViewController.
Any help appreciated!
The code i'm using is available here:
http://developer.apple.com/library/ios/#samplecode/iAdSuite/Introduction/Intro.html
My amended rootViewController.h:
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#interface RootViewController : UITableViewController <ADBannerViewDelegate>
#end
My amended rootViewController.m
#import "RootViewController.h"
#import "TextViewController.h"
#import "MapViewController.h"
#import "AdBannerNavigationAppDelegate.h"
// for SharedAdBannerView macro
// #define SharedAdBannerView ((AdBannerNavigationAppDelegate *)[[UIApplication sharedApplication] delegate]).adBanner
#import <iAd/iAd.h>
#interface RootViewController()
// Layout the Ad Banner and Content View to match the current orientation.
// The ADBannerView always animates its changes, so generally you should
// pass YES for animated, but it makes sense to pass NO in certain circumstances
// such as inside of -viewDidLoad.
- (void)layoutForCurrentOrientation:(BOOL)animated;
// A simple method that creates an ADBannerView
// Useful if you need to create the banner view in code
// such as when designing a universal binary for iPad
- (void)createADBannerView;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
[self createADBannerView];
[self layoutForCurrentOrientation:NO];
}
- (void)viewDidUnload
{
ADBannerView *adBanner = SharedAdBannerView;
adBanner.delegate = nil;
[adBanner removeFromSuperview];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self layoutForCurrentOrientation:NO];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self layoutForCurrentOrientation:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)dealloc
{
ADBannerView *adBanner = SharedAdBannerView;
adBanner.delegate = nil;
[adBanner removeFromSuperview];
[super dealloc];
}
- (void)createADBannerView
{
ADBannerView *adBanner = SharedAdBannerView;
NSString *contentSize;
if (&ADBannerContentSizeIdentifierPortrait != nil)
{
contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifierLandscape;
}
else
{
contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? ADBannerContentSizeIdentifier320x50 : ADBannerContentSizeIdentifier480x32;
}
CGRect frame;
frame.size = [ADBannerView sizeFromBannerContentSizeIdentifier:contentSize];
frame.origin = CGPointMake(0.0f, CGRectGetMaxY(self.view.bounds));
adBanner.frame = frame;
adBanner.delegate = self;
adBanner.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
adBanner.requiredContentSizeIdentifiers =
(&ADBannerContentSizeIdentifierPortrait != nil) ?
[NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil] :
[NSSet setWithObjects:ADBannerContentSizeIdentifier320x50, ADBannerContentSizeIdentifier480x32, nil];
[self.view addSubview:adBanner];
}
- (void)layoutForCurrentOrientation:(BOOL)animated
{
ADBannerView *adBanner = SharedAdBannerView;
CGFloat animationDuration = animated ? 0.2f : 0.0f;
CGRect contentFrame = self.view.bounds;
CGPoint bannerOrigin = CGPointMake(CGRectGetMinX(contentFrame), CGRectGetMaxY(contentFrame));
CGFloat bannerHeight = 0.0f;
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
adBanner.currentContentSizeIdentifier = (&ADBannerContentSizeIdentifierLandscape != nil) ? ADBannerContentSizeIdentifierLandscape : ADBannerContentSizeIdentifier480x32;
else
adBanner.currentContentSizeIdentifier = (&ADBannerContentSizeIdentifierPortrait != nil) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifier320x50;
bannerHeight = adBanner.bounds.size.height;
if (adBanner.bannerLoaded)
{
contentFrame.size.height -= bannerHeight;
bannerOrigin.y -= bannerHeight;
}
else
{
bannerOrigin.y += bannerHeight;
}
[UIView animateWithDuration:animationDuration
animations:^{
adBanner.frame = CGRectMake(bannerOrigin.x, bannerOrigin.y, adBanner.frame.size.width, adBanner.frame.size.height);
}];
NSLog(#"%f is y pos, height=%f, is it loaded...%#", adBanner.frame.origin.y, adBanner.frame.size.height, adBanner.bannerLoaded?#"YES":#"NO");
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[self layoutForCurrentOrientation:YES];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
[self layoutForCurrentOrientation:YES];
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
return YES;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
}
#end

Set Delegate methods on child view controll

I am building an application which should allow the user to scroll through the images. Since I have many images (downloaded from the web), what i doing is I have UP and down button on the parent view, in addition i have the scroll view. Based on the selected option (up or down), I add the ImageClass ( created a class which extends UIViewController) view to the scroll view.
Now, on the selected view the user can mark a point or do any stuff.
The question is how from the parent view I can call methods of the Uiviewcontroller. I know how I can set the delegate methods but what I want is that parent controller can call any a method say redraw method which would redraw the entire view.
Code:
-(IBAction) down:(id) sender{
[scrollView2 removeFromSuperview];
}
-(IBAction) down :(id) sender {
[scrollView2 removeFromSuperview];
if(scrollView2 == nil)
scrollView2 = [[UIScrollView alloc] init];
[self.view addSubview:scrollView2];
[self.view sendSubviewToBack:scrollView2];
[scrollView2 setBackgroundColor:[UIColor blackColor]];
[scrollView2 setCanCancelContentTouches:NO];
scrollView2.clipsToBounds = YES;
// default is NO, we want to restrict drawing within our scrollview
scrollView2.indicatorStyle = UIScrollViewIndicatorStyleWhite;
// CSImageView is a class which of the type UIViewController
imageVie = [[CSImageView alloc] init];
[scrollView2 addSubview:imageVie.view];
[scrollView2 setContentSize:CGSizeMake(1500,1500)];
[scrollView2 setScrollEnabled:YES];
[imageView release];
}
Now, from the parent view controller I want to call say:
imageVie.redraw(); method
Code for CSImageView
#interface CSImageView : UIViewController {
NSNumber *imageId ;
}
#property (nonatomic, retain) NSNumber *venueId;
-(void) redraw;
#end
#implementation CSImageView
#synthesize imageId;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) redraw {
NSLog(#"I am ehre in the test function");
}
#end
Can you please help me on the same. I am not able to call the redraw method. Any input would be appreciated.
First, CSImageView should probably be a subclass of UIView, not UIViewController. You should keep a reference to your instance (i.e. as an instance variable or synthesized property) so you can access it from methods other than - down:.
Wow.
I can't say I follow what you've coded, but here's how you do it.
During this part...add a tag to the view
// CSImageView is a class type UIViewController
imageVie = [[CSImageView alloc] init];
imageVie.tag = 99 // Any number. Preferably a constant
[scrollView2 addSubview:imageVie.view];
...
Then on the parent
view controller. You can ...
-(IBAction) down:(id) sender {
CSImageView *view = [scrollView2 viewWithTag:99];
[view redraw];
... }
Something like that.