Call method from another ViewController

I have ViewControllerA and ViewControllerB. I want to call the ViewControllerA method from ViewControllerB.

There is a method in ViewControllerA:

-(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 does not call. What for? thanks in advance

+6
source share
4 answers

You are lacking

 self. 

So far, something in viewControllerB:

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

then

 self.mutableArray =[self.viewControllerA loadData]; 

will work.

+7
source

Verify that the loadData method is specified in the viewController.B header file.

 - (void)loadData; 

After that, you can now call the loadData method.

 [viewControllerA loadData]; 
+4
source

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

0
source

When pushing controller B from controller A .. just specify

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

and then call method A from B. The task you are faced with is the non-distribution and just the "viewControllerA" declaration that you created in B.

0
source

Source: https://habr.com/ru/post/927544/


All Articles