2011-09-29 61 views
0

共享对象我还是在这个目标C新的东西,我尝试使用面向对象编程(OOP)清理我的代码。我无法共享的对象是在我第一类中创建我的第二类:目标C:与其他类

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以访问该属性? 谢谢

+0

MapView的是1类的一个实例变量。这意味着除非存在实例,否则它不存在。 –

回答

0

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

你可以通过使用“属性”来实现这一点,这些属性是带有(半)自动声明的getter和setter方法的特殊实例变量。

+0

我明白你在说什么,但我不知道如何将此传递给第二课。在第一堂课中这样做? Marker * mark = [[Marker alloc] init]; [mark addMarker:37.83:-96.58:mapView];所以这种方式将mapView作为参数传递。 –

+0

感谢我通过传递对象作为参数来获得它。 –