Invoke method in another class

2019-08-31 05:18发布

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.

However, I don't know how to invoke a method in viewControllerB's class!

1条回答
Lonely孤独者°
2楼-- · 2019-08-31 05:43

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:

@interface MyViewController : UIViewController {
    MyViewController *altViewController;
}

@property (nonatomic, retain) MyViewController *altViewController
@end

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];

查看更多
登录 后发表回答