Single ADBannerViewDelegate for multiple views - ios

I'm trying to implement simgle AdBanner instance for multiple views in an iOS app. For implementing AdbannerDelegate in a viewController one has to do
bannerview.delegate= self;
where bannerview is an instance of AdBannerView. This delegate method however has to be implemented in every viewController which amounts up to a lot of repeating code. How can I make up a simple class that implements all delegate methods and then I call use them in every viewController.

I think the viewControllers you are using are subclasses of UIViewController.
And you are saying all the viewControllers have the same delegate methods.
So,what i want to do is create new ViewController class (UIDelgateViewController) by SubClassing UIViewController and add all delegate methods there , and have all the other viewControllers subclass UIDelgateViewController.
The code goes like this,
.h file->
#interface UIDelegateViewController : UIViewController<ADBannerViewDelegate>
#property ADBannerView *bannerView;
#end
.m file ->
#import "UIDelegateViewController.h"
#interface UIDelegateViewController ()
#end
#implementation UIDelegateViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_bannerView = [[ADBannerView alloc] init];
_bannerView.delegate =self;
}
-(void)bannerDelegateMethod{
}
Now your Some viewController ->
#import "UIDelegateViewController.h"
#interface SomeViewController : UIDelegateViewController
#end
#import "SomeViewController.h"
#interface SomeViewController ()
#end
#implementation SomeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view addSubview:self.bannerView];
self.bannerView.frame = ..../
}

If you want to keep always on the screen the same banner while navigating and changing views, you should consider to use View Controller containtment API
A great example is that remarkable sample code written by Apple, that shows how to keep the same banner instance while moving in a tabbar or navigation controller. It could be also a great start for you project.

Related

unwind segue for multiple views

I am working on an app in Xcode, using modal segues to navigate forwards and using unwind segues to go back to the previous view and also to the home view. However, the segue is just not working when I open up the simulator, no errors are shown and I have used the same approach to unwind segue as is used in the Apple To-do list tutorial, so not sure why this isn't working.
Below is my .h file for one of the views I want to unwind from:
#import <UIKit/UIKit.h>
#interface XYZBonBonoftheDayViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *BBoD;
- (IBAction)unwindBBoD:(UIStoryboardSegue *)segue;
#end
Here is my .m file from the same view:
#import "XYZBonBonoftheDayViewController.h"
#interface XYZBonBonoftheDayViewController ()
#end
#implementation XYZBonBonoftheDayViewController
- (IBAction)unwindBBoD:(UIStoryboardSegue *)segue
{
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
If anyone could help, this would be immensely helpful! Thanks in advance :)

how to access function one class to another objective c [duplicate]

This question already has answers here:
Accessing Method from other Classes Objective-C
(8 answers)
Closed 9 years ago.
This is my viewcontroller1.m buttons userinteractionenabled function,
-(void) BtnUserInteractionStatus {
oneBtn2.userInteractionEnabled = NO;
oneBtn3.userInteractionEnabled = NO;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self BtnUserInteractionStatus];
}
This is my viewcontroller2.m buttons userinteractionenabled function access from viewcontroller1.m,
#import "ViewController1.h"
- (void)viewDidLoad {
[super viewDidLoad];
svc = [[StageViewController alloc]init];
[svc BtnUserInteractionStatus];
}
my viewcontroller2.h,
#import "ViewController1.h"
#interface ViewController2 : UIViewController {
StageViewController *svc;
}
#property (assign) StageViewController *svc;
But, this is not working. If there is any wrong please correct it.
This will be your ViewController2.m
#import "ViewController1.h"
- (void)viewDidLoad
{
[super viewDidLoad];
svc = [[ViewController1 alloc]initWithNibName:#"ViewController1" bundle:nil];
[self.view addSubview:svc.view];
}
ViewController2.h
#import "ViewController1.h"
#interface ViewController2 : UIViewController
{
ViewController1 *svc;
}
ViewController1.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
[self BtnUserInteractionStatus]; /*Provide your call to BtnUserInteractionStatus within init function*/
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(void)BtnUserInteractionStatus
{
oneBtn2.userInteractionEnabled = NO;
oneBtn3.userInteractionEnabled = NO;
}
Whenever you want the object library elements of a viewController loaded from another ViewContoller you need to define its xib and then add its corresponding view as either a subview of the present viewcontroller or push the viewcontrller from the current ViewController .Simply calling the BtnUserInteractionStatus method using its currently created instance wont help..
Hope it Helps !!

setting (custom) delegate runs into infinite loop

Am trying to write a simple custom delegate for displaying multiple selection list (after referring various online tutorials, stackoverflow, Apple doc), but in the class that I want to use the delegate, the line where I set the delegate runs into an infinite loop when I run it.
I have shared the source code here
https://bitbucket.org/ikosmik/uilistviewcontroller/src/ddfcd140b52e6e59d84e58d34d601f8f850145a1/UIList?at=master
UIListViewController (where am declaring the protocols)
https://bitbucket.org/ikosmik/uilistviewcontroller/src/ddfcd140b52e6e59d84e58d34d601f8f850145a1/UIList/UIListViewController.h?at=master
And am trying to use the delegate in a UIViewController called View_Exporter
#import <UIKit/UIKit.h>
#import "UIListViewController.h"
#interface View_Exporter : UIViewController <UIListViewDelegate, UIListViewDataSource>
#property (nonatomic, strong) IBOutlet UIView *viewForList;
#property (nonatomic, strong) UIListViewController *listViewController;
#end
View_Exporter.m
#import "View_Exporter.h"
#implementation View_Exporter
#synthesize arraySelectedList;
#synthesize viewForList;
#synthesize listViewController;
#pragma mark - UIListViewController Methods
-(NSArray *) itemsForList {
NSLog(#"View_Exporter itemsForList");
NSArray *array = [NSArray arrayWithObjects:#"Server", #"Memory", nil];
return array;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.listViewController = [[UIListViewController alloc] initWithNibName:#"UIListViewController" bundle:nil];
self.listViewController.listViewDelegate = self;
//[self.viewForList addSubview:self.listViewController.view];
self.listViewController.listViewDataSource = self;
}
#end
But this line in viewDidLoad seems to loop infinitely when I run the code :
self.listViewController.listViewDelegate = self;
Why is this looping infinitely? Am breaking my head since yesterday on this. not sure where am going wrong. can someone please help?
You've written a custom setter for listViewDelegate, at the end of this method you do this:
self.listViewDelegate = delegate;
This just calls the setter method again. Accessing a property via self. is just a way of calling[self setXX:xxx]. In your accessor method you need to set the instance variable directly, in the normal case this would be just
_delegate = delegate;
(The _delegate instance variable is created for you automatically). You can safely remove all of your synthesize statements, they aren't needed any more.

iOS 5: troubles with UITabBarControllerDelegate

Header file:
#import <UIKit/UIKit.h>
#interface PFXTabControllerViewController : UITabBarController <UITabBarControllerDelegate>
#end
Implementation file:
#import "PFXTabControllerViewController.h"
#interface PFXTabControllerViewController ()
#end
#implementation PFXTabControllerViewController
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(#"tabBar didSelectItem");
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(#"tabBarController didSelectViewController");
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBarController.delegate = self; // Tried it both with and without this.
}
The UITabBarcontroller shows just fine, with the tab bar. Clicking on a tab fires didSelectItem but it does not fire didSelectViewController which is the event I need to handle. The UITabBarcontroller is loading and showing different subclasses of UIViewController.
UPDATE Changing that self.tabBarController.delegate = self line to self.delegate = self works, and both fire. I don't understand why this is necessary. Setting self.delegate = self seems... silly. Any ideas?
Just change the header as follows :
#import <UIKit/UIKit.h>
#interface PFXTabControllerViewController : **UIViewController** <UITabBarControllerDelegate>
#end

viewDidLoad never called in Sub-classed UIViewController

I've run into some trouble with loading a sub-classed UIViewController from a nib. My viewDidLoad function is never called.
The super-class has no nib, but the sub-classes each have their own. i.e.
#interface SuperClass : UIViewController {
}
#end
#interface SubClass : SuperClass{
}
#end
#implementation SubClass
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad{
// Never called
}
The view is loaded from a nib as follows:
SubClass *scvc = [[SubClass alloc] initWithNibName:#"SubClass" bundle:nil];
[self.navigationController pushViewController:scvc animated:YES];
[scvc release];
There is a nib file with this name and it has it's file owner's class set properly.
viewDidLoad is not called in the child or super. Any ideas?
You say you are creating views in code (using loadView) in the super class, but trying to use a nib in the sub class?
According to the UIViewController docs (emphasis mine)
If you specify views using a nib file, you must not override loadView but should instead create a nib file in Interface Builder...
It looks like you may have a conflict there. As a test, comment out the loadView method in your superclass and see where that gets you.
I'm assuming SuperClass implements loadView. So when your SubClass is asked to loadView, you get your SuperClass' implementation, which overrides the normal nib loading mechanism.
I would rethink your design here. Your SuperClass' behavior is quite different from what you want your SubClass to do. Maybe that's not the best relationship there.
But, if you want, you should be able to at least make it work by doing this in your SubClass.m:
-(void)loadView {
IMP defaultImp = class_getMethodImplementation([[self superclass] superclass], _cmd);
/* or alternatively...
IMP defaultImp = class_getMethodImplementation([UIViewController class], _cmd);
*/
defaultImp(self, _cmd);
}
This implements a loadView for your subclass that skips over the loadView of your SuperClass and calls the default implementation instead.
IMO, this is ugly and might need to revisited if your class hierarchy expanded. I wouldn't do this in my own app, I would rethink the class hierarchy instead.
Where is this block located
SubClass *scvc = [[SubClass alloc] initWithNibName:#"SubClass" bundle:nil];
[self.navigationController pushViewController:scvc animated:YES];
[scvc release];
Are you sure it is called ?
I made a testproject to test it and the following works:
In your appDelegate.m file i put the following in the application didFinishLaunchingWithOptions method
UINavigationController *navController = [[UINavigationController alloc] init];
self.window.rootViewController = navController;
SubClass *viewController1 = [[[SubClass alloc] initWithNibName:#"SubClass" bundle:nil] autorelease];
[navController pushViewController:viewController1 animated:YES];
SuperClass.h
#import <UIKit/UIKit.h>
#interface SuperClass : UIViewController
#end
SuperClass.m
#import <UIKit/UIKit.h>
#import "SuperClass.h"
#implementation SuperClass
#end
SubClass.h
#import <UIKit/UIKit.h>
#import "SuperClass.h"
#interface SubClass : SuperClass
#end
SubClass.m
#import <UIKit/UIKit.h>
#import "SubClass.h"
#implementation SubClass
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
NSLog(#"Methods %# called", NSStringFromSelector(_cmd));
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#end
This works for me, make sure you set the right class for your nibFile aswell. Click on the file's owner icon and change the class from UIViewController to SubClass
Try to add:
[super viewDidLoad];

Resources