0

I have eventually succeeded with getting the index for the annotation clicked on at callOutAccecoryControlTapped-method. :) I have made a custom annotation class called Annotation (Annnotation *ann) which I fill with values from my db at parse.com. But now I don't know how to pass the values on to my next view LAOpeningHoursViewController? I can see all the values when I am debugging and *ann (se code) has for example: ann.cname="Olles cafe" etc. So that is good.

I pass the whole ann-object on to my next view. But I don't know how to get for example ann.cname on to my 'self.myLabel.text'. I show you the code:

If you have the kindness to answer me, please explain very well because I am a beginner. Best is if you could have the trouble writing a code-snippet here… ;)

This is some code of the first view:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];
if (!pinView) {
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
    pinView.pinColor = MKPinAnnotationColorRed;
    pinView.animatesDrop = YES;
    pinView.canShowCallout = YES;

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinView.rightCalloutAccessoryView = rightButton;

} else {
    pinView.annotation = annotation;
}
return pinView;
}


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

    Annotation *ann = [[mapView selectedAnnotations] objectAtIndex:0];

    NSLog(@"ann.subtitle = %@", ann.subtitle);
    NSLog(@"ann.cid = %@", ann.cid);
    NSLog(@"ann.cstr = %@", ann.cstr);
    NSLog(@"ann.cname = %@", ann.cname);
    //this works fine, I get all the values

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


}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"openingHours"]) {

    LAOpeningHoursViewController *secondView =     (LAOpeningHoursViewController*)segue.destinationViewController;
    //PFObject *tempObject = [[myMapView selectedAnnotations] objectAtIndex:0];
    Annotation *ann = (id)sender;

    //Here is the problem: What to pass? I want to pass for example ann.cname that is    declared in my custom annotation
    //Both 'ann' and 'tempObject' has the right values for cname but I dont know how to pass it to the next view
    secondView.myLabel.text = self.stringToPass;
   //secondView.myLabel.text = ann.cname;
       }
    }

And here is the code from the second view called LAOpeningHoursViewController, that should receive the data:

- (void)viewDidLoad
{
[super viewDidLoad];


        self.myLabel.text = ann.cname;
        //recieving an error-code… of course 

}

declared in LAOpeningHoursViewController.h as this

 @property (strong, nonatomic) IBOutlet UILabel *myLabel;
 @property (strong, nonatomic) NSString *textContent;

and finally, here is my Annotation.h-file where cname is the one that should go to 'myLabel'

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

@interface Annotation : NSObject <MKAnnotation>
@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy)NSString *title;
@property(nonatomic, copy)NSString *subtitle;
@property(nonatomic, copy)UIButton *rightButton;

@property(nonatomic, retain)NSString *cid;
@property(nonatomic, retain)NSString *cname;
@property(nonatomic, retain)NSString *ctel;
@property(nonatomic, retain)NSString *cstr;
@property(nonatomic, retain)NSString *cmon;
@property(nonatomic, retain)NSString *ctue;
@property(nonatomic, retain)NSString *cwed;
@property(nonatomic, retain)NSString *passedId;
@property(nonatomic, retain)NSString *objectId;


@end
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Hot Licks Oct 05 '14 at 12:23
  • Thank You @Hot Licks but I need to understand how I can get values out of my custom class Annotation *ann from the first view to my second. I want to write ann.cname but I get 'property not found on object of type PFObject'. Now it just don't work and it drives me crazy :) – Kardemumma09 Oct 05 '14 at 13:30
  • And do you include Annotation.h in LAOpeningHoursViewController.m? (And how is `ann` declared>) – Hot Licks Oct 05 '14 at 13:52
  • Thanks for your comment! Is it an `#import "Annotations.h"`I should do in the LAOpeningsViewController.h? I don't know who told me but I have a ´@class LAGoogleViewController;` there which is my sending view. Is that right or should I take it away? When I write secondView.myLabel.text = self.stringToPass, there is nil in secondView.myLabel.text. In my secondView I have declared `@property (strong, nonatomic) IBOutlet UILabel *myLabel;` (connected to the label on my storyboard) In my firstViewController I have declared stringToPass as this: `@property (strong, nonatomic) NSString*stringToPass;` – Kardemumma09 Oct 05 '14 at 14:04
  • @Hot Licks I just took away to send 'ann'. I've changed it as an experiment to [self performSegueWithIdentifier: @"openingHours" sender:self];and in prepareForSegue I just have secondView.myLabel.text = self.stringToPass instead of declaring Annotation *ann = (id)sender; a second time.For short: I just want to send the content of `self.stringToPass`to my second views `myLabel`. Please help me. Got to finish this today :) – Kardemumma09 Oct 05 '14 at 15:00

1 Answers1

0

The sender parameter in [self performSegueWithIdentifier: @"openingHours" sender:ann]; should be set to self. This is intended to give the presented view controller reference to the presenting controller.

Add the property @property (nonatomic, strong) Annotation *selectedAnnotation to the presenting view controller. So that it can be set thus:

//this works fine, I get all the values

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

Now you have the selected annotation as a property it can be used to initialize the LAOpeningHoursViewController:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"openingHours"]) {

    LAOpeningHoursViewController *secondView =     (LAOpeningHoursViewController*)segue.destinationViewController;
    //PFObject *tempObject = [[myMapView selectedAnnotations] objectAtIndex:0];
    Annotation *ann = (id)sender;

    // Setup second view here:
    secondView.myLabel.text = self.selectedAnnotation.cname;
    // assign further properties here...
    }
}

I hope this helps, good luck!

Rowan Jones
  • 552
  • 5
  • 7