简体   繁体   English

从AppDelegate访问类的运行实例

[英]Accessing a class' running instance from the AppDelegate

New to Obj-C, I'm trying to trigger a method myMethod of an instance of viewControllerB. Obj-C的新手,我试图触发viewControllerB实例的方法myMethod。 =>viewControllerB is instanciated in viewControllerA. => viewControllerB在viewControllerA中实例化。 =>I'm trying to call myMethod from the AppDelegate =>It seems that anyway I try to do that, It's triggering myMethod in a new instance of viewControllerB =>我正在尝试从AppDelegate调用myMethod =>似乎无论如何我都试图这样做,它在viewControllerB的新实例中触发myMethod

So the question is: how to send a message to an existing instance of a viewController ? 所以问题是:如何将消息发送到viewController的现有实例?

Please explain with code samples since I'm not yet really comfortable with Obj-C. 由于我对Obj-C还不太满意,请提供代码示例进行解释。

Thanks ! 谢谢 !

viewControllerA.m : viewControllerA.m:

if (_viewControllerB == nil) {
    self.viewControllerB = [[[ViewControllerB alloc] initWithNibName:@"ViewControllerB" bundle:[NSBundle mainBundle]] autorelease];
}

viewControllerB.m : viewControllerB.m:

- (void)myMethod{ NSLog(@"myMethod!"); }

AppDelegate.m : Don't know what tu put here :( AppDelegate.m:不知道您在这里放什么:(

To get same instance 要获得相同的实例

for (UIViewController *view in self.navigationController.viewControllers) 用于(UIViewController * self.navigationController.viewControllers中的视图)

{ {

if(view isKindOfClass:[viewControllerB class])
{
     [view yourmethodname];
}

} }

如果要在viewControllerB的同一实例上调用该函数,则必须从添加它的位置取出该实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM