开发者

How can a method tell which view controller called it

I want to get the current view controller i开发者_StackOverflow中文版n 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


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

UIViewController *currentVC = self.navigationController.visibleViewController;


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

      }
}


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜