Passing the selected location between two UI View Maps

Discussion in 'iOS Programming' started by teranceTT, Feb 23, 2014.

  1. teranceTT macrumors newbie

    Joined:
    Feb 23, 2014
    #1
    Hello,

    I have two UI Map Views. The user selects the location from the first one and the selected location is passed to the second ui map view (where it is displayed). In order to store the selected location, I have created primitive singleton class (I am at the beginning process of studying the iOS programming).

    However, the selected location doesn't appear at the second UI View Map controller. Would you be so kind to tell where is my mistake? Here is the link to the github: https://github.com/terancet/Projects_GeoChat
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    Post your relevant code here. It's unlikely someone will download your github work and wade through it trying to figure out what your issue is. Use the CODE tag to wrap you code.
     
  3. teranceTT thread starter macrumors newbie

    Joined:
    Feb 23, 2014
    #3
    Class where I store information about the selected place:

    Code:
    #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
    
    
    [B]//implementation of the above class[/B]
    #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:
    Code:
    .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
    
    [B]/*------------part of the implementation class--------*/[/B]
    //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

    Code:
    #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
    
    [B]/*----------------------------.m---------------------------------------*/[/B]
    #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];
    
    [B]    //displays nothing[/B]
        NSLog(@"%@",[_info.annotation title]);
    [B]    //however, when I write 
        //NSLog(@"%@",_info.annotation);
        //It displays me that the object is not nill
        //adds nothing to the map[/B]
        [_map addAnnotation:_info.annotation];
    }
    @end
    
     

Share This Page