Messaging between two classes
I have a basic question about fetching values through different classes. I have a classA which fills an array (If i print it out it is not empty). LATER in class B i want to load this Array: I call a function from class A which returns the Array of class A. But in class B if i call my new array then is it null. I am a bit confused, because i think i retain every value of the array, but its still null. I tried also a lot of different possibilities. I think its a basic OOP syntax fault i produce?!
//CLASS_A.h
@interface classA {
NSMutableArray* buoyArray;
}
@property (nonatomic, retain) NSMutableArray * buoyArray;
-(NSMutableArray*)getArray:(NSMutableArray*)_array;
//CLASS_A.m
...
-(NSMutableArray*)getArray:(NSMutableArray*)_array {
_array=buoyArray;
return _array;
}
//CLASS_B.h
#import "CLASS_A.h"
@class classA;
@interface classB ...
classA *mapSource;
NSMutableArray * buoyArray;
}
@property(nonatomic开发者_如何学Python,retain) classA *mapSource;
//CLASS_B.m
buoyArray=[mapSource getArray:buoyArray];
NSLog(@"%@",buoyArray);
Actually you you are making new object of class A by calling alloc so by init it reintialize all properties values for that instance.
What you need,if you are pushing class B over class A then, fetch existing class A object from stack, by using this line.
mapSource = (ClassA *)[self.navigationController.viewControllers objectAtIndex: [self.navigationController.viewControllers count]-2];
then call this
buoyArray=[mapSource getArray:buoyArray];
NSLog(@"%@",buoyArray);
精彩评论