调用另一类中的方法 [英] Invoke method in another class

查看:78
本文介绍了调用另一类中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有各自视图的视图控制器(viewControllerAviewControllerB).当用户触摸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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆