user198725878
user198725878

Reputation: 6386

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

Upvotes: 6

Views: 22057

Answers (3)

EmptyStack
EmptyStack

Reputation: 51374

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

UIViewController *currentVC = self.navigationController.visibleViewController;

Upvotes: 39

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31720

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")])
      {  

      }
}

Upvotes: 12

saadnib
saadnib

Reputation: 11145

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

Upvotes: 3

Related Questions