0

I run into a problem with prepareforsegue-style. I am, as you can tell of my question quite new to Xcode, and I just don't get this right:

I have a LAFirstView reading data from parse that I save in an array called 'kundUppgifter'.

Now I need to send a text from the objectatindex:index to the next view that is called LASecondView. In the LASecondView I have declared a label that is called myLabel where I want the text to show up.

I have been reading about it here at Stackoverflow, but the problem is that when I read others answers I don't understand what to replace things with. My code in the LAFirstView :looks like this and of course it is not working but I do get the right things out from NSLog. Hoping for help:

//IMPLEMENT THIS ONE (WHEN DISCLOSURE BUTTON IS PRESSED):
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view  calloutAccessoryControlTapped:(UIControl *)control{

[self performSegueWithIdentifier: @"openingHours" sender: self];

NSUInteger index = [mapView.annotations indexOfObject:view.annotation];
PFObject *tempObject = [kundUppgifter objectAtIndex:index];
NSLog(@"%lu", (unsigned long)index);
NSLog(@"%@", [tempObject objectForKey:@"CNAME"]);

}
  • Do not rely on the order of the map view's `annotations` array -- it is not guaranteed to be in any order. Instead, keep whatever data is needed (or at least references to it) in the annotation object itself (ie. view.annotation). For the segue part, see http://stackoverflow.com/questions/14805954/mkannotationview-push-to-view-controller-when-detaildesclosure-button-is-clicked. –  Sep 29 '14 at 01:31
  • Yes Anna, thank You, I discovered that it is not any order.:( I have found that by doing this: NSString *passedId = view.annotation.title, I get the title which I have at parse.com under the @"cname" which a passed to an array called kundUppgifter. So instead of setting this: PFObject *tempObject = [kundUppgifter objectAtIndex:index] I would like to sett this (but it is not working:error code): PFObject *tempObject = [kundUppgifter objectForKey passedId]; I don't get how to do what you suggest with view.annotation because I am so new to Xcode.Can you explain little more? :D – Kardemumma09 Sep 29 '14 at 22:20

1 Answers1

1

So your problem is that it does not pass the string from LAFirstView to LASecondView, but the logs are correct? If that's the case, you can declare a string property on LAFirstView:

@property (strong, nonatomic) NSString* stringToPass;

Then on disclosure button click:

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

NSUInteger index = [mapView.annotations indexOfObject:view.annotation];
PFObject *tempObject = [kundUppgifter objectAtIndex:index];
NSLog(@"%lu", (unsigned long)index);
NSLog(@"%@", [tempObject objectForKey:@"CNAME"]);

self.stringToPass = [tempObject objectForKey:@"CNAME"];

[self performSegueWithIdentifier: @"openingHours" sender: self];

}

And prepareForSegue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"openingHours"]) {
        LASecondView *secondView = (LASecondView *)segue.destinationViewController;
        secondView.labelToUpdate.text = self.stringToPass;
    }
}
aj_f
  • 932
  • 7
  • 12
  • Hello! I discovered that it is not any order on the index. I have found that by doing this: NSString *passedId = view.annotation.title, I get the title which I have at parse.com under the @"cname" which a passed to an array called kundUppgifter. So instead of setting this: PFObject *tempObject = [kundUppgifter objectAtIndex:index] I would like to sett this (but it is not working:error code): PFObject *tempObject = [kundUppgifter objectForKey passedId]; – Kardemumma09 Sep 29 '14 at 22:23
  • It seems to me that it is necessary to create a NSDictionary as I am adding the custom annotation.cname and annotation.cstreet and annotation.objectId to my custom annotation class called 'Annotation'. – Kardemumma09 Oct 03 '14 at 22:34
  • When I write secondView.myLabel.text = self.stringToPass, there is nil in secondView.myLabel.text – Kardemumma09 Oct 05 '14 at 13:38