Reputation: 1310
How can I call the one method in all the view controllers?
One method in viewcontroller1 like,
- (void)doSearch
{
NSLog(@"Search..!");
}
I want to call doSearch
method from viewcontroller2, viewcontroller3, viewcontroller4, viewcontroller5, etc.
How to do this?
Upvotes: 0
Views: 1056
Reputation: 20021
The best way is to make a separate header file make this method a class method and you can call this anywhere in the project by importing the class.And you can include this class into your pch file so that import can be avoided from all VCs
class.h
+ (void)doSearch
{
NSLog(@"Search..!");
}
in ViewControllers
#import "class.h"
-(void)someMethod
{
[class doSearch];
}
Upvotes: 0
Reputation: 4208
You can declare this method in AppDelegate.h
. like
- (void)doSearch;
And Implement it in AppDelegate.m
like
- (void)doSearch
{
//Your Search Logic
}
Then, Create AppDelegate
instance like
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
Make Your Project's .pch
file look like following:
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
AppDelegate *appDelegate;
#endif
Now, From Any ViewController
, You can call method like:
[appDelegate doSearch];
Happy Coding.
NOTE: Personally, I avoid this kind of implementation in AppDelegate
Class.
I use Singleton Object Pattern for this kind of purpose. But, Given is the quickest way.
___________EDIT____________
Add a New Class file of NSObject
type: Name it CommonMethod
(or whatever you want)
in CommonMethod.h
@interface CommonMethods : NSObject
+ (CommonMethods *)sharedObject;
+ (void)doSearch;
in CommonMethod.m
#import "CommonMethods.h"
@implementation CommonMethods
+ (CommonMethods *)sharedObject
{
static dispatch_once_t once;
static CommonMethods *sharedObject;
dispatch_once(&once, ^ { sharedObject = [[CommonMethods alloc] init]; });
return sharedObject;
}
+ (BOOL)doSearch
{
// Your Search Logic.
}
Now, Add #import "CommonMethods.h"
to your project's .pch
file.
And You are all set to go...!!!
Method Call (in any of your viewController): [CommonMethods doSearch];
Upvotes: 0
Reputation: 12671
You can declare it in separate class and instantiate that class in all viewControllers, or you could define this method in your AppDelegate
and call in all your viewController. you could access the AppDelegate in your ViewControllers by getting its instance like this
self.appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
and finally call the method like this
[self.appDelegate doSearch];
Generally it is preferable to declare all your methods or data which is shared through the application in a separate class and use this class. I usually use singelton
Object class in my application, define all the shared data in that and finally access in all classes
here is example of singelton class
MyData.h
@interface MyData : NSObject
+(MyData*)getInstance;
-(void)search;
@end
MyData.m
#import "MyData.h"
@implementation MyData
static MyData *instance =nil;
+(MyData *)getInstance
{
@synchronized(self)
{
if(instance==nil)
{
instance= [[MyData alloc]init];
}
}
return instance;
}
-(void)search {
NSLog(@"search");
}
@end
finally in your viewController
MyData *myData=[MyData getInstance];
[myData search];
Upvotes: 1
Reputation: 7113
The best way to make AbstractViewController
and inside it add all your common behaviors or methods. Any ViewController
will inherits from the Abstract and can call any common Method
Upvotes: 0
Reputation: 35783
You may like to read below tutorial
Global functions and variables in Objective C
or You can create methods in the appDelegate class that you can access through all your app. For example, in your AppDelegate.h you have declared this method as follow:
-(void)myMethod;
In your AppDelegate.m, you define it as:
-(void)myMethod
{
NSLog(@"myMethod is getting called");
}
From any other classes (Master or Detail, etc.), you access myMethod by:
#import "AppDelegate.h"
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate myMethod];
Upvotes: 0
Reputation: 47059
Add your method to AppDelegate.m
file
- (void)doSearch
{
NSLog(@"Search..!");
}
And Add #import "AppDelegate.h"
In UIViewController
that you want to call :
You can call method by,
AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[del performSelector:@selector(doSearch) withObject:nil afterDelay:0];
From Any UIViewController
Upvotes: 0
Reputation: 18865
Put all controllers in an NSArray *
, lets say it is called controllerArray
.
Then you can make all controllers perform selector with:
[controllerArray makeObjectsPerformSelector:@selector(doSearch)];
Upvotes: 0