当我点击Pin没有得到正确的数据。解?

时间:2014-11-28 16:58:22

标签: ios objective-c xcode mkmapview mkannotationview

我在点击某个引脚时遇到问题没有发送正确的数据。根据我的理解,即使它有一个NSUInteger,它们总是在变化。我该如何解决?

代码是:

DisplayMap.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
#import <Parse/Parse.h>

@interface DisplayMap : NSObject <MKAnnotation> 

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *icon;


@property (nonatomic, strong) NSString *Telefono;
@property (nonatomic, strong) NSString *Email;
@property (nonatomic, strong) NSString *Sito;
@property (nonatomic, strong) NSString *TipologiaLocale;
@property (nonatomic, strong) NSString *Cucina;
@property (nonatomic, strong) NSString *Vegano;
@property (nonatomic, strong) NSString *Valutazione;
@property (nonatomic, strong) NSString *Latitudine;
@property (nonatomic, strong) NSString *Longitudine;
@property (nonatomic, strong) NSString *FaceB;
@property (nonatomic, strong) NSString *Twit;

@property (nonatomic, strong) PFFile *Anteprima1;
@property (nonatomic, strong) PFFile *Anteprima2;
@property (nonatomic, strong) PFFile *Anteprima3;

@end

DisplayMap.m

#import "DisplayMap.h"


@implementation DisplayMap

@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
@synthesize icon;

@end

Alberghi.h

#import <UIKit/UIKit.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <MapKit/MapKit.h>
#import <Parse/Parse.h>

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

#import "Reachability.h"
#import "TestFlight.h"
#import "MBProgressHUD.h"
#import "DisplayMap.h"
#import "Recipe.h"
#import "DettagliAlberghi.h"

@interface Alberghi : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> {

    MKMapView *_mapView;
}

@property (strong, nonatomic) IBOutlet CLLocationManager *locationManager;
@property (strong, nonatomic) IBOutlet CLGeocoder *geoCoder;

- (IBAction)TornaHome:(id)sender;

@end

Alberghi.m

#import "Alberghi.h"

@interface Alberghi ()

@end

@implementation Alberghi

@synthesize locationManager;
@synthesize geoCoder;

- (void)viewDidLoad {
    [super viewDidLoad];

        _mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 65, 320, 415)];
        [_mapView setDelegate:self];
        _mapView.layer.cornerRadius = 5;

        locationManager.delegate = self;
        self.locationManager = [[CLLocationManager alloc] init];

        if(IS_OS_8_OR_LATER) {

            [self.locationManager requestWhenInUseAuthorization];
            [self.locationManager requestAlwaysAuthorization];
        }

        [self.locationManager startUpdatingLocation];

        _mapView.showsUserLocation = NO;
        [_mapView setMapType:MKMapTypeStandard];
        [_mapView setZoomEnabled:YES];
        [_mapView setScrollEnabled:YES];
        [self.view addSubview:_mapView];


        PFQuery *query = [PFQuery queryWithClassName:@"Ristoranti"];

        [query orderByDescending:@"createdAt"];

        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

            if (!error)
            {

                //NSLog(@"QUERY -----> :%@", objects);


                for(NSDictionary *note1 in objects) {
                    float realLatitude1 = [[note1 objectForKey:@"Latitudine"] floatValue];
                    float realLongitude1 = [[note1 objectForKey:@"Longitudine"] floatValue];

                    NSLog(@"(PARSE) Latitudine: %f", realLatitude1);
                    NSLog(@"(PARSE) Longitudine: %f", realLongitude1);

                    DisplayMap *displayMap = [[DisplayMap alloc] init];

                    CLLocationCoordinate2D theCoordinate;
                    theCoordinate.latitude = realLatitude1;
                    theCoordinate.longitude = realLongitude1;
                    displayMap.coordinate = theCoordinate;
                    displayMap.title = [note1 objectForKey:@"NomeLocale"];
                    displayMap.subtitle = [note1 objectForKey:@"Indirizzo"];
                    displayMap.icon = [note1 objectForKey:@"PinMappa"];
                    [_mapView setDelegate:self];
                    [_mapView addAnnotation:displayMap];
                }
            }
        }];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if (![annotation isKindOfClass:[DisplayMap class]])
    {

        return nil;
    }

    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";

    MKAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];

    if (pinView == nil)
    {
        pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];

    }
    pinView.canShowCallout = YES;
    pinView.annotation = annotation;

    DisplayMap *myAnn = (DisplayMap *)annotation;
    pinView.image = [UIImage imageNamed:myAnn.icon];



    // Create a UIButton object to add on the
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [pinView setRightCalloutAccessoryView:rightButton];

    return pinView;
}

- (void)mapView:(MKMapView *)mapView
 annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure)
    {
        [self performSegueWithIdentifier:@"Prova" sender:view];


    }
}

#pragma mark - mappa

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    for (MKAnnotationView *annotationView in views) {

        if (annotationView.annotation == mapView.userLocation) {
            MKCoordinateSpan span = MKCoordinateSpanMake(1.4, 1.4);
            MKCoordinateRegion region = MKCoordinateRegionMake(mapView.userLocation.coordinate, span);
            [mapView setRegion:region animated:YES];
        }
    }
}

- (IBAction)TornaHome:(id)sender
{

    [self performSegueWithIdentifier:@"TornaHome" sender:self];
}



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"Prova"]) {



        MKAnnotationView *annotationView = sender;



        DettagliAlberghi *destViewController =(DettagliAlberghi *) segue.destinationViewController;

        DisplayMap *displayMap = (DisplayMap *)annotationView.annotation;

        NSLog(@"DisplayMap? %@", displayMap);


        NSLog(@"Dettagli Ristorante:\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@
        \n%@", displayMap.title, displayMap.subtitle, displayMap.Telefono, displayMap.Email, 
        displayMap.Sito, displayMap.TipologiaLocale, displayMap.Cucina, displayMap.Vegano, 
        displayMap.Valutazione, displayMap.Latitudine, displayMap.Longitudine, 
        displayMap.Anteprima1, displayMap.Anteprima2, displayMap.Anteprima3, displayMap.FaceB,
        displayMap.Twit);

        destViewController.recipe = displayMap;






        /*
        MKAnnotationView *annotationView = sender;

        NSLog(@"Esporto i dati: %@", annotationView);

        DettagliAlberghi *destViewController = segue.destinationViewController;

        DisplayMap *displayMap = [[DisplayMap alloc] init];

        displayMap.title = [sender objectForKey:@"NomeLocale"];
        displayMap.subtitle = [sender objectForKey:@"Indirizzo"];
        displayMap.icon = [sender objectForKey:@"PinMappa"];
        displayMap.Telefono = [sender objectForKey:@"Telefono"];
        displayMap.Email = [sender objectForKey:@"Email"];
        displayMap.Sito = [sender objectForKey:@"Sito"];
        displayMap.TipologiaLocale = [sender objectForKey:@"TipologiaLocale"];
        displayMap.Cucina = [sender objectForKey:@"Cucina"];
        displayMap.Vegano = [sender objectForKey:@"Vegano"];
        displayMap.Valutazione = [sender objectForKey:@"Valutazione"];
        displayMap.Latitudine = [sender objectForKey:@"Latitudine"];
        displayMap.Longitudine = [sender objectForKey:@"Longitudine"];
        displayMap.Anteprima1 = [sender objectForKey:@"Anteprima1"];
        displayMap.Anteprima2 = [sender objectForKey:@"Anteprima2"];
        displayMap.Anteprima3 = [sender objectForKey:@"Anteprima3"];
        displayMap.FaceB = [sender objectForKey:@"Facebook"];
        displayMap.Twit = [sender objectForKey:@"Twitter"];




        NSLog(@"Dettagli Ristorante: \n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@", displayMap.title, displayMap.subtitle, displayMap.Telefono, displayMap.Email, displayMap.Sito, displayMap.TipologiaLocale, displayMap.Cucina, displayMap.Vegano, displayMap.Valutazione, displayMap.Latitudine, displayMap.Longitudine, displayMap.Anteprima1, displayMap.Anteprima2, displayMap.Anteprima3, displayMap.FaceB, displayMap.Twit);




        destViewController.recipe = displayMap;
        */

    }
}

@end

当我点击图钉时,应用程序崩溃了。我在PrepareToSegue中添加了一个NSLog,我注意到View中导出的所有字段都是空的,但是,数据是通过viewDidLoad中的PFQuery读取的。怎么了?当我点击引脚不导出所需的数据?

2014-11-29 08:08:16.190 Veg[1083:8473] (PARSE) Latitudine: 45.435745
2014-11-29 08:08:16.190 Veg[1083:8473] (PARSE) Longitudine: 10.986951
2014-11-29 08:08:16.191 Veg[1083:8473] (PARSE) Latitudine: 45.441578
2014-11-29 08:08:16.191 Veg[1083:8473] (PARSE) Longitudine: 10.982130
2014-11-29 08:08:29.102 Veg[1083:8473] DisplayMap? <DisplayMap: 0x7f90335b71f0>
2014-11-29 08:08:29.102 Veg[1083:8473] Dettagli Ristorante: 
La Lanterna
Piazzetta Portichetti, 6 Verona
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
2014-11-29 08:08:29.301 Veg[1083:8473] Double Coord: 0.000000, 0.000000
2014-11-29 08:08:31.356 Veg[1083:8473] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

我无法理解如何订购...每次打开视图时始终以相同的顺序始终改变位置,因此当我点击Pin视图时,我会打开不同的而不是您想要的那个。解决方案?提前致谢

1 个答案:

答案 0 :(得分:1)

更新,在viewDidLoad中添加它(在ViewController中):

            displayMap.icon = [note1 objectForKey:@"PinMappa"];

            // Add this

            displayMap.Telefono = [note1 objectForKey:@"Telefono"];
            displayMap.Email = [note1 objectForKey:@"Email"];
            displayMap.Sito = [note1 objectForKey:@"Sito"];
            displayMap.FaceB = [note1 objectForKey:@"Facebook"];
            displayMap.Twit = [note1 objectForKey:@"Twitter"];
            displayMap.Cucina = [note1 objectForKey:@"Cucina"];
            displayMap.TipologiaLocale = [note1 objectForKey:@"TipologiaLocale"];
            displayMap.Valutazione = [note1 objectForKey:@"Valutazione"];
            displayMap.Vegano = [note1 objectForKey:@"Vegano"];

            // End of adding

            [_mapView setDelegate:self];

您好我假设您正在使用ARC,我担心您已经从非常旧的字体中复制了代码,实际上是其中的几个。 (例如,你有三个实例变量名称是标题,一个是协议,其中一些是从属性创建另一个,合成错误)。而dealloc方法最好不要说。 把你的代码更改为:(别担心它有你需要的相同变量(没有重复)。

DispalyMap.h:

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

 @interface DisplayMap : NSObject <MKAnnotation>

 @property (nonatomic, copy) NSString *icon;


@property (nonatomic, strong) NSString *Telefono;
@property (nonatomic, strong) NSString *Email;
@property (nonatomic, strong) NSString *Sito;
@property (nonatomic, strong) NSString *TipologiaLocale;
@property (nonatomic, strong) NSString *Cucina;
@property (nonatomic, strong) NSString *Vegano;
@property (nonatomic, strong) NSString *Valutazione;
@property (nonatomic, strong) NSString *Latitudine;
@property (nonatomic, strong) NSString *Longitudine;
@property (nonatomic, strong) NSString *FaceB;
@property (nonatomic, strong) NSString *Twit;

@property (nonatomic, strong) PFFile *Anteprima1;
@property (nonatomic, strong) PFFile *Anteprima2;
@property (nonatomic, strong) PFFile *Anteprima3;

@end

Display.m :(只有这个)

@implementation DisplayMap

@synthesize coordinate = _coordinate;
@synthesize title = _title;
@synthesize subtitle = _subtitle;

@end

好吧,我需要帮助你崩溃消息(因为如果这个改变你的应用程序不起作用,可能错误可能在新的视图控制器中)。

//老答案:

我无法理解你的代码100%,因为缺少一些代码,但我认为你做了很多你不需要的东西。试试这个方法,(评论你当前的方法,然后粘贴)。

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (![annotation isKindOfClass:[DisplayMap class]])
{

    return nil;
}

static NSString* AnnotationIdentifier = @"AnnotationIdentifier";

MKAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];

if (pinView == nil)
{
    pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];

}
    pinView.canShowCallout = YES;
    pinView.annotation = annotation;

DisplayMap *myAnn = (DisplayMap *)annotation;
pinView.image = [UIImage imageNamed:myAnn.icon];



// Create a UIButton object to add on the
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[pinView setRightCalloutAccessoryView:rightButton];

return pinView;
}



 - (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

 if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure)
 {
    [self performSegueWithIdentifier:@"Prova" sender:view];


}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"Prova"]) {

    MKAnnotationView *annotationView = sender;



    DettagliAlberghi *destViewController =(DettagliAlberghi *) segue.destinationViewController;

    DisplayMap *displayMap = (DisplayMap *)annotationView.annotation;



    destViewController.recipe = displayMap;
}
}