Objective C: Sharing an object with another class
I'm still new at this Objective C stuff and I'm trying to clean up my code using Object Oriented Programming (OOP). I'm having trouble sharing an object to my second class that was created in my first class:
开发者_开发问答Class1.h
@interface Class1ViewController : UIViewController {
IBOutlet RMMapView *mapView;
}
@property (nonatomic, retain) RMMapView *mapView;
@end
I Originally had this function in Class1.m that I want to move over to Class2.m and clean up the code:
@implementation Marker
- (void)addMarker:(NSInteger)lat:(NSInteger)lon{
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:@"marker-red" ofType:@"png"];
UIImage *imgLocation = [[UIImage alloc] initWithContentsOfFile:fileLocation];
RMMarker *markerCurrentLocation = [[[RMMarker alloc] initWithUIImage:imgLocation] autorelease];
markerCurrentLocation.zPosition = -1.0;
CLLocationCoordinate2D startingPoint;
startingPoint.latitude = lat;
startingPoint.longitude = lon;
//This line I'm having trouble with, the mapView object from Class1
[mapView.contents.markerManager addMarker:markerCurrentLocation AtLatLong:startingPoint];
[markerCurrentLocation release];
[imgLocation release];
markerCurrentLocation = nil;
}
@end
How I access the object mapView that is on Class1? Do I need to instantiate Class 1 to access the property? Thanks
An object pointer is a value, just like an int
or float
. You can call from an instance of your first class to an instance of your second class and pass this value as a parameter. Then store it in an instance variable inside the instance of the second class.
You can automate this somewhat by using "properties", which are special instance variables with (semi) automatically declared getter and setter methods.
精彩评论