Methode wird nicht aufgerufen

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Methode wird nicht aufgerufen

    Moin Zusammen,

    ich verzweifle hier dran gerade. Meine Methode

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

    wird nicht aufgerufen. mapView-Delegate hat keine Auswirkungen.

    Die Annotations werden angelegt und angezeigt. Ich möchte in der Methode aber noch einen Button erzeugen, mit dem ich dann einen Segue ausführe.


    Quellcode

    1. UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    2. [rightButton setFrame:CGRectMake(0,0,32,32)];
    3. [rightButton setEnabled:YES];
    4. annotationView.rightCalloutAccessoryView = rightButton;

    Ich mache alles programmatisch ohne Storyboard.

    Wo hab ich denn was vergessen?

    LG
    Bernd
    Ich bin gegen Signaturen!!!
  • Bei solchen Phänomenen prüfe ich zunächst immer, ob andere ganz normale Delegate Methoden aufgerufen werden. Beispielsweise - mapView:didAddAnnotationViews:
    Und prüf, ob das Objekt, welches Du gerade erwartest, auch wirklich das Delegate Deiner MapView ist.

    Der Klassiker: das Ding wird über eine xib geladen, in der ich das Delegate Outlet setze und dann setze ich im Code noch mal ein anderes Delegate…
    «Applejack» "Don't you use your fancy mathematics to muddle the issue!"

    Iä-86! Iä-64! Awavauatsh fthagn!

    kmr schrieb:

    Ach, Du bist auch so ein leichtgläubiger Zeitgenosse, der alles glaubt, was irgendwelche Typen vor sich hin brabbeln. :-P
  • Marco Feltmann schrieb:

    Quellcode

    1. - (NSMutableArray *)createAnnotations
    2. {
    3. NSMutableArray *annotations = [[NSMutableArray alloc] init];
    4. NSString *path = [[NSBundle mainBundle] pathForResource:@"Infopunkte" ofType:@"plist"];
    5. NSArray *locationsArray = [NSArray arrayWithContentsOfFile:path];
    6. for (NSDictionary *row in locationsArray) {
    7. NSNumber *latitude = [row objectForKey:@"latitude"];
    8. NSNumber *longitude = [row objectForKey:@"longitude"];
    9. NSString *title = [row objectForKey:@"title"];
    10. NSString *subtitle = [row objectForKey:@"subtitle"];
    11. // NSString *image = [row objectForKey:@"image"];
    12. CLLocationCoordinate2D coord;
    13. coord.latitude = latitude.doubleValue;
    14. coord.longitude = longitude.doubleValue;
    15. MapViewAnnotation *annotation = [[MapViewAnnotation alloc] initWithTitle:title AndCoordinate:coord];
    16. annotation.subtitle = subtitle;
    17. [annotations addObject:annotation];
    18. }
    19. return annotations;
    20. }
    21. - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
    22. NSLog(@"Annotation added!");
    23. }
    24. - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    25. NSString *identifier = @"Annotations";
    26. MKAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    27. annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    28. annotationView.enabled = YES;
    29. annotationView.canShowCallout = YES;
    30. UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    31. [rightButton setFrame:CGRectMake(0,0,32,32)];
    32. [rightButton setEnabled:YES];
    33. annotationView.rightCalloutAccessoryView = rightButton;
    34. if ([annotation.title isEqualToString:@"Barbarossahöhle"]) {
    35. annotationView.image = [UIImage imageNamed:@"barbarossahoehle_mapicon.png"];
    36. } else if ([annotation.title isEqualToString:@"Schlosspark"] && [annotation.subtitle isEqualToString:@"Bendeleben"]) {
    37. annotationView.image = [UIImage imageNamed:@"bendeleben_schlosspark_mapicon.png"];
    38. } else if ([annotation.title isEqualToString:@"Orangerie"] && [annotation.subtitle isEqualToString:@"Bendeleben"]) {
    39. annotationView.image = [UIImage imageNamed:@"bendeleben_orangerie_mapicon.png"];
    40. } else {
    41. return nil;
    42. }
    43. return annotationView;
    44. }
    45. - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    46. if ([view.annotation.title isEqualToString:@"Barbarossahöhle"]) {
    47. NSLog(@"Barbarossahöhle");
    48. } else if ([view.annotation.title isEqualToString:@"Schlosspark"]) {
    49. NSLog(@"Schlosspark");
    50. } else if ([view.annotation.title isEqualToString:@"Orangerie"]) {
    51. NSLog(@"Orangerie");
    52. } else {
    53. NSLog(@"nix");
    54. }
    55. }
    Alles anzeigen
    Dateien
    • IMG_2647.PNG

      (516,15 kB, 236 mal heruntergeladen, zuletzt: )
    Ich bin gegen Signaturen!!!
  • hatte ja auch im ersten Beitrag geschrieben das sie angezeigt werden ohne das addAnnotation, würden sie ja auch nicht angezeigt werden

    ich hab in einem alten Projekt nur addAnnotation genutzt und die einzelnd hinzugepackt (keine Ahnung warum ich nicht die Methode mit dem Array genutzt habe)

    hast du es mal mit einer einzelnen Annotation versucht?
    Ich weiß nicht immer wovon ich rede aber ich weiß das ich Recht habe. :saint:
  • MCDan schrieb:


    Quellcode

    1. mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, 300)];
    2. [mapView addAnnotations:[self createAnnotations]];


    InfopunkteViewController.h

    Quellcode

    1. #import <UIKit/UIKit.h>
    2. #import <MapKit/MapKit.h>
    3. #import <CoreLocation/CoreLocation.h>
    4. @interface InfopunkteViewController : UIViewController <UIApplicationDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, MKMapViewDelegate, CLLocationManagerDelegate>
    5. @property (nonatomic, strong) UICollectionView *cView;
    6. @property (nonatomic, strong) NSDictionary *cellDic;
    7. @property (nonatomic, strong) NSArray *cellArray;
    8. @property (nonatomic, strong) MKMapView *mapView;
    9. @property (nonatomic, strong) CLLocationManager *locationManager;
    10. @end
    Alles anzeigen

    InfopunkteViewController.m

    Quellcode

    1. #import "InfopunkteViewController.h"
    2. #import "MapViewAnnotation.h"
    3. @interface InfopunkteViewController ()
    4. @end
    5. @implementation InfopunkteViewController
    6. @synthesize cellDic, cellArray, mapView, cView;
    7. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    8. {
    9. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    10. if (self) {
    11. // Custom initialization
    12. }
    13. return self;
    14. }
    15. - (void)viewDidLoad
    16. {
    17. [super viewDidLoad];
    18. self.locationManager = [[CLLocationManager alloc] init];
    19. self.locationManager.delegate = self;
    20. mapView.delegate = self;
    21. mapView.showsUserLocation = YES;
    22. CGRect scrollViewFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    23. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
    24. scrollView.scrollEnabled = YES;
    25. scrollView.clipsToBounds = YES;
    26. [self.view addSubview:scrollView];
    27. mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, 300)];
    28. [mapView addAnnotations:[self createAnnotations]];
    29. [scrollView addSubview:mapView];
    30. [self zoomToLocation];
    31. [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"naviback.png"] forBarMetrics:UIBarMetricsDefault];
    32. [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:1 green:1 blue:1 alpha:1.0], NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:18]}];
    33. [self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1.0]];
    34. NSString *path = [[NSBundle mainBundle]pathForResource:@"Infopunkte" ofType:@"plist"];
    35. cellArray = [[NSArray alloc] initWithContentsOfFile:path];
    36. UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    37. cView=[[UICollectionView alloc] initWithFrame:CGRectMake(20,360,self.view.frame.size.width-40,self.view.frame.size.height) collectionViewLayout:layout];
    38. [cView setDataSource:self];
    39. [cView setDelegate:self];
    40. [cView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    41. [cView setBackgroundColor:[UIColor clearColor]];
    42. [scrollView addSubview:cView];
    43. scrollView.contentSize = CGSizeMake(self.view.frame.size.width, mapView.frame.size.height + cView.frame.size.height);
    44. }
    Alles anzeigen
    Ich bin gegen Signaturen!!!
  • Ich bin jetzt etwas verwirrt. Das Setzen des delegate in Zeile 28 bringt ja nicht viel, wenn Du den MapView erst in Zeile 37 erzeugst. ?(

    Dir ist schon klar, dass man ein Objekt benötigt, um die Properties bei diesem setzen/verändern zu können? ;)

    Edit: Ah ok, jetzt verstehe ich. Der Post war ja von gestern, also vom 1. April. :thumbsup:
  • self wird seit ARC beim Zugriff auf Properties nicht mehr benötigt.

    Ich finde es jedoch sehr verwirrend, wenn man Properties per @synthesize auf die namen, also ohne vorgestelltes _, mappt. Damit ist dann nicht mehr ersichtlich, ob man auf ein Property oder eine lokale Variable zugreift. :(
  • MCDan schrieb:

    Ich bin jetzt etwas verwirrt. Das Setzen des delegate in Zeile 28 bringt ja nicht viel, wenn Du den MapView erst in Zeile 37 erzeugst.
    Naja, doch. Zumindest bis Zeile 37, wenn das WebView als gesetztes Outlet aus der Xib kommt.

    Gut, der Post war von gestern, der Code aber von heute. Wenn, dann hat beage sich selbst nachhaltig in den April geschickt. ;)
    «Applejack» "Don't you use your fancy mathematics to muddle the issue!"

    Iä-86! Iä-64! Awavauatsh fthagn!

    kmr schrieb:

    Ach, Du bist auch so ein leichtgläubiger Zeitgenosse, der alles glaubt, was irgendwelche Typen vor sich hin brabbeln. :-P
  • MCDan schrieb:

    self wird seit ARC beim Zugriff auf Properties nicht mehr benötigt.

    Ich finde es jedoch sehr verwirrend, wenn man Properties per @synthesize auf die namen, also ohne vorgestelltes _, mappt. Damit ist dann nicht mehr ersichtlich, ob man auf ein Property oder eine lokale Variable zugreift. :(
    Echt? Man lernt nie aus. ;)
    Ich war mir eigentlich ziemlich sicher, mir immer noch regelmässig Warnings einzufangen wenn ich mal wieder irgendwo ein "self." vergessen habe.

    Sprich ich hätte jetzt vermutet, dass er auch zusätzlich zur vertauschten Reihenfolge der Befehle auch den Setter umgeht,
    da er wie du erwähnt hast die iVar von _map nach map umbenannt hat.
  • MCDan schrieb:

    self wird seit ARC beim Zugriff auf Properties nicht mehr benötigt.
    Aus Speicherverwaltungsgründen nicht mehr, aber dann hebelst du KVO aus. Obwohl, Swift erledigt das ja auch von alleine.

    Thyraz schrieb:

    Ich war mir eigentlich ziemlich sicher, mir immer noch regelmässig Warnings einzufangen wenn ich mal wieder irgendwo ein "self." vergessen habe.
    Vermutlich wenn du innerhalb eines Blocks auf Instanzvariablen zugreifst.