1

I have the singleton class that stores the property - selected location (MKPointAnnotation). This location is set from the first map view by the user. However, I don't grasp why this property is null? Here the code.

Link to the project

The selected pieces of the code.

Class where I store information about the selected place:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface GeoChatInformationAboutTheSelectedPlace : NSObject <MKMapViewDelegate>
//create object that mimic the Place with the required information
@property (nonatomic)         NSUInteger numberOfThePostsForTheSelectedLocation;
@property (nonatomic, strong) MKPointAnnotation *annotation;
+(GeoChatInformationAboutTheSelectedPlace *)returnInstance;
@end


//implementation of the above class
#import "GeoChatInformationAboutTheSelectedPlace.h"

@implementation GeoChatInformationAboutTheSelectedPlace
static GeoChatInformationAboutTheSelectedPlace *info=NULL;
+(GeoChatInformationAboutTheSelectedPlace *)returnInstance{
    if (!info){
        info = [[GeoChatInformationAboutTheSelectedPlace alloc] init];
        info.annotation=[[MKPointAnnotation alloc] init];
    }
    return info;
}
@end

How I add the selected location (based on Anna's comments):

.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "GeoChatInformationAboutTheSelectedPlace.h"
@interface GeoChatMapViewController : UIViewController <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *map;
@property (strong,nonatomic) NSMutableArray *matchingItems;
@property (strong, nonatomic) IBOutlet UITextField *textSearch;
@property (strong, nonatomic) GeoChatInformationAboutTheSelectedPlace *info;
@property (weak, nonatomic) IBOutlet UIStepper *stepper;
- (IBAction)increaseDecreaseRadiousOfTheSearch:(id)sender;
@end

/*------------part of the implementation class--------*/
//selecting the location and displaying its coordinates
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Would you like to add this location?"
                                                    message:[view.annotation title]
                                                   delegate:self
                                          cancelButtonTitle:@"Yep!"
                                          otherButtonTitles:@"No :(", nil];
    [alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex==0) {
        MKAnnotationView *selectedAnnotation=[_map.selectedAnnotations objectAtIndex:0];

        if ([selectedAnnotation isKindOfClass:[MKPointAnnotation class]]) {
            MKPointAnnotation *pa = (MKPointAnnotation *)selectedAnnotation;
            //[_info.arrayOfTheSelectedPlaces addObject:pa];
            _info.annotation=pa;
        }
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _info=[GeoChatInformationAboutTheSelectedPlace returnInstance];
    _map.showsUserLocation=YES;
}

How I display the selected annotation at the second map:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "GeoChatInformationAboutTheSelectedPlace.h"
@interface GeoChatProjectSavedLocationToDisplayOnMapViewController : UIViewController <MKMapViewDelegate>
@property (strong, nonatomic) IBOutlet MKMapView *map;
@property (strong, nonatomic) GeoChatInformationAboutTheSelectedPlace *info;
@property (strong, nonatomic) MKPointAnnotation *bb;
@end

/*----------------------------.m---------------------------------------*/
#import "GeoChatProjectSavedLocationToDisplayOnMapViewController.h"

@interface GeoChatProjectSavedLocationToDisplayOnMapViewController ()
@end

@implementation GeoChatProjectSavedLocationToDisplayOnMapViewController
-(void)setBb:(MKPointAnnotation *)bb{
    _bb=bb;
}
-(void)setMap:(MKMapView *)map {
    _map=map;
    _map.delegate=self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    _map.showsUserLocation=YES;
    _info=[GeoChatInformationAboutTheSelectedPlace returnInstance];

    //displays nothing
    NSLog(@"%@",[_info.annotation title]);
    //however, when I write 
    //NSLog(@"%@",_info.annotation);
    //It displays me that the object is not nill
    //adds nothing to the map
    [_map addAnnotation:_info.annotation];
}
@end
Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
mr.M
  • 851
  • 6
  • 23
  • 41
  • The code is hard to follow and does not compile (it's incomplete -- it doesn't even show or I don't see where the first VC creates the second VC and "passes" the data to the singleton). It's not clear why you need a singleton for this in the first place. The singleton code also looks suspicious. –  Feb 23 '14 at 14:30
  • However, in didSelectAnnotationView, code is trying to set the _properties_ of `_buffedAnnotationPoint` but where is `_buffedAnnotationPoint` _itself_ created or set? If `_buffedAnnotationPoint` itself is nil, setting its properties does nothing. –  Feb 23 '14 at 14:31
  • Please note you probably don't need `_buffedAnnotationPoint` at all since you can get a reference to the selected annotation using the map view's selectedAnnotations property (see [this](http://stackoverflow.com/questions/11783880/access-an-alertviews-calling-view/11784222#11784222) and [this](http://stackoverflow.com/questions/20775282/edit-annotation-text-with-an-uialertviewstyleplaintextinput/20775804#20775804)). –  Feb 23 '14 at 14:32
  • Also: In clickedButtonAtIndex, the code sets `_info.annotation` but is `_info` itself already created or set? –  Feb 23 '14 at 14:33
  • I have posted the git hub. Please check it. – mr.M Feb 23 '14 at 15:48

0 Answers0