0

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

这是我的代码:

位置视图.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

位置视图.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
4

1 回答 1

1

Why dont you simply synthesize the properties of your AddressAnnotation class and use something like

addAnnotation.mTitle = yourNewString;

Do I get your question wrong? This seems to be too obvious by far.

Edit

Of course, you have to give this back. For example like this:

- (NSString *)title{ return mTitle;}
于 2012-05-31T07:41:32.113 回答