Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a method tell which view controller called it

I want to get the current view controller in my own method. I mean i have two view controllers which are calling a same method. In that i want to diffentiate from which view controller class is calling that method.

Please help me out

like image 369
user198725878 Avatar asked Jul 01 '11 04:07

user198725878


3 Answers

If it is a navigation based app, you can get the current view controller by,

UIViewController *currentVC = self.navigationController.visibleViewController;
like image 78
EmptyStack Avatar answered Oct 23 '22 06:10

EmptyStack


Lets say myCommonMethod: is the common function called from both the view controller , you could check your viewController whether it's the member of a class or not using isMemberOfClass: method of NSObject.

-(void) myCommonMethod:(UIViewController*) aViewController
{
      if([aViewController isMemberOfClass:NSClassFromString(@"MyFirstController")])
      {
      }
      else if([aViewController isMemberOfClass:NSClassFromString(@"MySecondController")])
      {  

      }
}
like image 27
Jhaliya - Praveen Sharma Avatar answered Oct 23 '22 07:10

Jhaliya - Praveen Sharma


If both of your view controllers are calling same function then you can pass self as a parameter in that method for this you can write function as -

-(void) functionName:(UIViewController*) viewController
like image 3
saadnib Avatar answered Oct 23 '22 06:10

saadnib