如何在ViewDidLoad中更改注释的标题?

时间:2012-05-31 06:45:42

标签: objective-c xcode mkannotation

我有mkannotation的代码,但我不知道如何在ViewDidLoad中更改标题。

这是我的代码:

LocationView.h

...
@interface AddressAnnotation : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;

    NSString *mTitle;
    NSString *mSubTitle;

}

@end

@interface LocationView : UIViewController<MKMapViewDelegate> {

    IBOutlet MKMapView *mapView;

    AddressAnnotation *addAnnotation;

}

@property (nonatomic, retain) Offers *offer;

@property (retain, nonatomic) IBOutlet MKMapView *mapView;


-(CLLocationCoordinate2D) addressLocation;


@end

LocationView.m

#import "LocationView.h"

@implementation AddressAnnotation

@synthesize coordinate;

- (NSString *)subtitle{
    return @"Sub Title";
}
- (NSString *)title{
    return @"Title";
}

-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
    coordinate=c;
    //NSLog(@"%f,%f",c.latitude,c.longitude);
    return self;
}

@end

@interface LocationView ()

@end

@implementation LocationView

@synthesize mapView, offer;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // I want in this method to change title

    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta=0.2;
    span.longitudeDelta=0.2;

    CLLocationCoordinate2D location = [self addressLocation];
    region.span=span;
    region.center=location;

    if(addAnnotation != nil) {
        [mapView removeAnnotation:addAnnotation];
        addAnnotation = nil;
    }

    addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];

    [mapView addAnnotation:addAnnotation];

    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];


    //[mapView selectAnnotation:mLodgeAnnotation animated:YES];


}


- (void)viewDidUnload
{
    [super viewDidUnload];


}

//:(MKMapView *)mapView

-(CLLocationCoordinate2D) addressLocation {


    double latitude = 0.0;
    double longitude = 0.0;

    if(offer.lat > 0 && offer.lon > 0) {
        latitude = [offer.lat doubleValue];
        longitude = [offer.lon doubleValue];
    }


    CLLocationCoordinate2D location;
    location.latitude = latitude;
    location.longitude = longitude;

    return location;
}

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];

    annView.pinColor = MKPinAnnotationColorGreen;
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);

    return annView;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

1 个答案:

答案 0 :(得分:1)

为什么不简单地合成AddressAnnotation类的属性并使用类似

的内容
addAnnotation.mTitle = yourNewString;

我的问题是否错误?到目前为止,这似乎太明显了。

修改

当然,你必须回馈它。例如:

- (NSString *)title{ return mTitle;}