0

I have code for mkannotation but I don't know how to change title in ViewDidLoad.

This is my code:

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
iWizard
  • 6,816
  • 19
  • 67
  • 103

1 Answers1

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;}
pbx
  • 1,137
  • 7
  • 15
  • Yes, it works now. Sorry but I'm new to objective-c :) Thank's for help – iWizard May 31 '12 at 07:47
  • No problem. If you think about it, it's logical. How shall your LocationView know about what you did to a private iVar in another class. You have to tell ;-) – pbx May 31 '12 at 07:51
  • Yes. I'm getting objective-c logic more and more. – iWizard May 31 '12 at 07:52