Calling method from another ViewController

2019-04-08 01:36发布

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

4条回答
萌系小妹纸
2楼-- · 2019-04-08 02:07

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.

查看更多
等我变得足够好
3楼-- · 2019-04-08 02:13

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];
查看更多
走好不送
4楼-- · 2019-04-08 02:13

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

查看更多
太酷不给撩
5楼-- · 2019-04-08 02:20

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.

查看更多
登录 后发表回答