gpt4 book ai didi

iphone - 从 AppDelegate 访问类的运行实例

转载 作者:行者123 更新时间:2023-11-29 05:05:29 25 4
gpt4 key购买 nike

Obj-C 新手,我正在尝试触发 viewControllerB 实例的 myMethod 方法。=>viewControllerB在viewControllerA中实例化。=>我正在尝试从 AppDelegate 调用 myMethod=>看来无论如何我都尝试这样做,它在 viewControllerB 的新实例中触发 myMethod

所以问题是:如何向 viewController 的现有实例发送消息?

请用代码示例进行解释,因为我还不太熟悉 Obj-C。

谢谢!

viewControllerA.m:

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

viewControllerB.m:

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

AppDelegate.m:不知道你在这里放什么:(

最佳答案

获取相同的实例

for (UIViewController *view in self.navigationController.viewControllers)

{

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

关于iphone - 从 AppDelegate 访问类的运行实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5450409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com