How a method can determine which view controller called it

I want to get the current view controller in my own method. I mean, I have two view controllers that call the same method. In this I want to distinguish from which class controller class calls this method.

Please help me

+6
source share
3 answers

myCommonMethod: is a common function called from both controllers, you can check your viewController to see if it is a member of a class or does not use the isMemberOfClass: NSObject .

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

If this is a navigation based application, you can get the current view controller,

 UIViewController *currentVC = self.navigationController.visibleViewController; 
+39
source

If both of your view managers call the same function, you can pass self as a parameter in this method for this, you can write the function as -

 -(void) functionName:(UIViewController*) viewController 
+3
source

Source: https://habr.com/ru/post/891786/


All Articles