Calling method from another ViewController

半世苍凉 提交于 2019-12-06 01:43:14

问题


I have a ViewControllerA and a ViewControllerB. I want calling a method of ViewControllerA from ViewControllerB.

In ViewControllerA is present a method:

  -(NSMutableArray*) loadData;

In ViewControllerB.h:

 #import "ViewControllerA.h"
  .......
 @property (nonatomic, strong) ViewControllerA * viewControllerA;
 @property (nonatomic, strong) NSMutableArray * mutableArray;

In ViewControllerB.m:

self.mutableArray =[viewControllerA loadData];

but the method is not calling. Why? Thanks in advance


回答1:


You are missing

self.

As long as somewhere in viewControllerB:

self.viewControllerA = [[viewControllerA alloc]init];  //or some other initialization occurs...

then:

self.mutableArray =[self.viewControllerA loadData];

will work.




回答2:


Make sure that the method loadData is specified in viewControllerB's header file.

- (void)loadData;

After than, you can now call the method loadData.

[viewControllerA loadData];



回答3:


viewControllerA is allocated in ViewControllerB before calling [viewControllerA loadData]?




回答4:


While pushing controller B from controller A..just specify

viewControllerB.viewControllerA = self;
[self.navigationController pushViewController:viewControllerB animated:YES];

and then from B call the method A.The problem which you faced is due to non allocation and just declaratio of "viewControllerA " which you had created in B.



来源:https://stackoverflow.com/questions/12855931/calling-method-from-another-viewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!