ios 方法如何判断哪个视图控制器调用了它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6543501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can a method tell which view controller called it
提问by user198725878
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
请帮帮我
回答by EmptyStack
If it is a navigation based app, you can get the current view controller by,
如果它是基于导航的应用程序,您可以通过以下方式获取当前视图控制器,
UIViewController *currentVC = self.navigationController.visibleViewController;
回答by Jhaliya
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
.
让我们说myCommonMethod:
是从视图控制器调用的公共函数,您可以viewController
使用 的isMemberOfClass:
方法检查它是否是类的成员NSObject
。
-(void) myCommonMethod:(UIViewController*) aViewController
{
if([aViewController isMemberOfClass:NSClassFromString(@"MyFirstController")])
{
}
else if([aViewController isMemberOfClass:NSClassFromString(@"MySecondController")])
{
}
}
回答by saadnib
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 -
如果您的两个视图控制器都调用相同的函数,那么您可以self
在该方法中作为参数传递,您可以将函数编写为 -
-(void) functionName:(UIViewController*) viewController