问题
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