调用另一类中的方法 [英] Invoke method in another class
问题描述
我有两个具有各自视图的视图控制器(viewControllerA
和viewControllerB
).当用户触摸viewControllerA
视图中的按钮时,我可以加载viewControllerB
的视图.
I have two view controllers (viewControllerA
and viewControllerB
) with their own views. When the user touches a button in the view of viewControllerA
, I am able to load the view of the viewControllerB
.
但是,我不知道如何在viewControllerB
的类中调用方法!
However, I don't know how to invoke a method in viewControllerB
's class!
推荐答案
没有太多要继续的信息,我将假定A和A B是同一类的(尽管不是必须的).您总是可以声明一个属性,然后将其从另一个指向另一个:
Without much info to go on I'll assume that both A & B are of the same class (though they wouldn't have to be). You could always declare a property that would then point from one to the other:
MyViewController.h:
MyViewController.h:
@interface MyViewController : UIViewController {
MyViewController *altViewController;
}
@property (nonatomic, retain) MyViewController *altViewController
@end
只要确保实例化后,就使用指向另一个VC的指针填充该属性.然后,您可以通过以下方法轻松地在另一个视图控制器上调用方法: [altViewController doMethod];
Just be sure that once instantiated you populate that property with a pointer to the other VC. Then you can easily call a method on the other view controller simply by: [altViewController doMethod];
这篇关于调用另一类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!