Gold Challenge issue- subtitle not showing


#1

I tried adding a subtitle to the designated initializer , not having much success. Any help would be appreciated.
The annotation only shows the title. When i plug the date string in place of the title the current date shows up, so, i know it’s not an issue with my dateString.

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface BNRMapPoint : NSObject <MKAnnotation>
{
}
//a new designated initializer for instances of BNRMapPoint
-(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t subTitle:(NSString *)st;

//this is a required property from MKAnnotaion
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

//this is an optional property
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subTitle;


@end

BNRMapPoint.m

#import "BNRMapPoint.h"

@implementation BNRMapPoint

@synthesize coordinate, title, subTitle;

-(id)initWithCoordinate:(CLLocationCoordinate2D)c
                  title:(NSString *)t
               subTitle:(NSString *)st
{
    self = [super init];
    if (self) {
        coordinate = c;
        [self setTitle:t];
        [self setSubTitle:st];
    }
    return self;
}

-(id)init
{
    return [self initWithCoordinate:CLLocationCoordinate2DMake(43.07, -89.32)
                              title:@"Hometown" subTitle:@"today"];
    
}

@end

WhereAmIViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "BNRMapPoint.h"

@interface WhereAmIViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate>

{
    CLLocationManager *locationMgr;
    
    IBOutlet MKMapView *worldView;
    IBOutlet UIActivityIndicatorView *activityIndicator;
    IBOutlet UITextField *locationTitleField;
    IBOutlet UISegmentedControl *segmentedControl;
    
}

-(IBAction)changeView:(id)sender;

@property(nonatomic) MKMapType mapType;


-(void)findLocation;
-(void)foundLocation:(CLLocation *)loc;



@end

WhereAmIViewController.m

#import "WhereAmIViewController.h"

@interface WhereAmIViewController ()

@end

@implementation WhereAmIViewController

-(IBAction)changeView:(id)sender
{
    switch (segmentedControl.selectedSegmentIndex) {
        case 0:
            worldView.mapType = MKMapTypeStandard;
            break;
        case 1:
            worldView.mapType = MKMapTypeSatellite;
            break;
        case 2:
            worldView.mapType = MKMapTypeHybrid;
            break;
            
        default:
            break;
    }
}
-(void)findLocation
{
    [locationMgr startUpdatingLocation];
    [activityIndicator startAnimating];
    [locationTitleField setHidden:YES];
}

-(void)foundLocation:(CLLocation *)loc
{
    CLLocationCoordinate2D coord = [loc coordinate];
    //create date string
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    
    NSDate *today = [NSDate date];
    NSString *stringDate = [dateFormatter stringFromDate:today];
   
    
    //create instance of bnrMapPoint with current data
    BNRMapPoint *mp = [[BNRMapPoint alloc] initWithCoordinate:coord title:[locationTitleField text] subTitle:stringDate];
    
    
    //add it to the map view
    [worldView addAnnotation:mp];
    
    //zoom the region to this location
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
    [worldView setRegion:region animated:YES];
    
    //reset the ui
    [locationTitleField setText:@""];
    [activityIndicator stopAnimating];
    [locationTitleField setHidden:NO];
    [locationMgr stopUpdatingLocation];
    
}



-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //create location mgr object
        locationMgr = [[CLLocationManager alloc] init];
        
        
        //ignore warning for now
        [locationMgr setDelegate:self];
                
        //bronze challenge
        [locationMgr setDistanceFilter:50];
        
        //tell the mgr to start 
        
        
    }
    return self;
    
}

-(void)viewDidLoad
{
    [worldView setShowsUserLocation:YES];
    [segmentedControl setHidden:NO];
    [worldView setZoomEnabled:YES];
    [worldView setScrollEnabled:YES];
    
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [worldView setRegion:region animated:YES];
    
    
}



-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"%@", newLocation);
    
    //How many seconds ago was this new location created?
    NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];
    //cllocationmanagers will return the last found locaton of the device first, you don't want that
    //data in this case. If this location was made more than 3 minutes ago, ignore it.
    if (t < -180) {
        //this is cached data, keep looking
        return;
    }
    [self foundLocation:newLocation];
    
    
}

-(void)locationManager:(CLLocationManager *)manager
      didFailWithError:(NSError *)error
{
    NSLog(@"Could not find location: %@", error);
}

-(void)dealloc
{
    
    [locationMgr setDelegate:nil];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [self findLocation];
    
    [textField resignFirstResponder];
    
    return YES;
}




@end

using xcode 4.5.2 iOS simulator 5.1


#2

Hmm… I’m not sure exactly what u’re doing wrong, but I just added

@property (nonatomic, copy) NSString *subtitle;

to my BNRMapPoint.h
and

[self setSubtitle:@"subtitle example"];

to BNRMapPoint.m and it works fine. Can’t see the faul logic in your file though too…


#3

I think the problem is a typo.

You wrote:

[self setSubTitle:st];
But MKAnnotation’s property is subtitle not subTitle. Give this a try:

[self setSubtitle:st]


#4

I made the same mistake originally. If you use: “subTitle” it will fail. You need to use “subtitle”.