Gold Challenge: Annotation Extras


#1

On this one I first just appended a date and time to the title property, and somewhat clumsily at that since I’m still getting used to string manipulation and dates in Objective C/Cocoa. After checking the docs though, there’s a handy subtitle property that seems like it’s begging to be used for this exercise.

…Hmm, apparently I also suck at getting PHPBB to format Objective C to look decent

- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t
{
    self = [super init];
    if (self) {
        // Make sure the creation date is initialized to "now"
        dateCreated = [[NSDate alloc] init];
        
        coordinate = c;
        [self setTitle:t];
        
        // Include the date in the subtitle
        [self setSubtitle:[NSString stringWithFormat:@"Tagged on: %@",
                        [NSDateFormatter localizedStringFromDate:dateCreated 
                                                       dateStyle:NSDateFormatterShortStyle 
                                                       timeStyle:NSDateFormatterShortStyle]]];
    }
    
    return self;
}

#2

I did the same, but I have splitted a little the code:

- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t
{
    self = [super init];
    
    if(self) {
        coordinate = c;
        [self setTitle:t];
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateStyle:NSDateFormatterShortStyle];
        
        NSDate *date = [NSDate date];
        
        [self setSubtitle:[NSString stringWithFormat:@"Created on %@", [formatter stringFromDate:date]]];
    }
    
    return self;
}

#3

In BNRMapPoint.h I added the optional subtitle property from MKAnnotation:

In BNRMapPoint.m I added the subtitle property to the designated initializer:

- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t subtitle:(NSString *)s { self = [super init]; if (self) { coordinate = c; [self setTitle:t]; [self setSubtitle:s]; } return self; }
In WhereamiViewController.m inside the foundLocation: method, I used the localizedStringFromDate:dateStyle:timeStyle: class method in the subtitle argument, which used the NSDate date class method for its date argument:

BNRMapPoint *mp = [[BNRMapPoint alloc] initWithCoordinate:coord title:[locationTitleField text] subtitle:[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle]];


#4

I’m trying to do it, but it doesn’t work. I can’t set the subtitle. I always get an error from the line 12 "No visible @interface for ‘BNRMapPoint’ declares the selector ‘setSubtitle:’

Here is the code :

[code]- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t
{
self = [super init];
if (self) {
coordinate = c;
[self setTitle:t];

    // I create a date with short style
    NSDateFormatter *form = [[NSDateFormatter alloc] init];
    [form setDateStyle:NSDateFormatterShortStyle];
    [form setTimeStyle:NSDateFormatterShortStyle];
    strDate = [form stringFromDate:[NSDate date]];
    
    [self setSubtitle:strDate];
}
return self;

}
[/code]

I created with properties a NSString *strDate :

@property (nonatomic, readwrite) NSString *strDate;
I also synced it.

Thanks for your help!


#5

@chalusf3 You are trying to access a property that hasn’t been declared in your code:

Add the subtitle property to your BNRMapPoint.h file:

Then synthesize the property in the BNRMapPoint.m file:@synthesize subtitle;

Now you should be able to set/get the subtitle.


#6

Thanks a lot!
Was it what Joe Conway would call an “optional property from MKAnnotation” ?


#7

[quote=“chalusf3”]Thanks a lot!
Was it what Joe Conway would call an “optional property from MKAnnotation” ?[/quote]

I would guess so, since title itself is optional. Although xcode reference didn’t explicitly use the word “optional”.