目标C:与另一个类共享一个对象

时间:2011-09-29 18:01:57

标签: objective-c

我仍然是这个Objective C的新手,我正在尝试使用面向对象编程(OOP)来清理我的代码。我无法将对象分享到我在第一堂课中创建的第二堂课:

Class1.h
@interface Class1ViewController : UIViewController {
     IBOutlet RMMapView *mapView;
}
@property (nonatomic, retain) RMMapView *mapView;
@end

我最初在Class1.m中有这个功能,我想转移到Class2.m并清理代码:

@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

我如何访问Class1上的对象mapView?我是否需要实例化Class 1才能访问该属性? 感谢

1 个答案:

答案 0 :(得分:0)

对象指针是一个值,就像intfloat一样。您可以从第一个类的实例调用第二个类的实例,并将此值作为参数传递。然后将它存储在第二个类的实例内的实例变量中。

您可以使用“属性”自动执行此操作,“属性”是具有(半)自动声明的getter和setter方法的特殊实例变量。