MKPinannotation detail disclosure button - present new view

 ̄綄美尐妖づ 提交于 2019-11-30 05:25:48

If I understand correctly, you want to add a disclosure button that would allow you to present a new view with information about the current pin annotation. To get a disclosure button, you just need to implement this code:

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

        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinView.rightCalloutAccessoryView = rightButton;
    } else {
        pinView.annotation = annotation;
    }
    return pinView;
}

Now when you tap on a pin on the mapView, a disclosure button will display in the presented view. You will then need to use the following method to tell the app what to do when the disclosure button is pressed.

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

In that method, you can present a popover or modal window or push a view or whatever you would like to do with information about the current location. You don't need to create a new nib for each one, either. The easiest thing to do would be to synthesize values in that view controller such as phoneNumber and website and things like that. Then in the last method there where you present the view, pass the values in as needed. For example,

NewView *vc = [[NewView alloc] initWithNibName:@"NewView" bundle:nil];
vc.phoneNumber = // the phone number of this location;
vc.website = // the website;

and so on before you present the view. I hope this helps

EDIT: To get rid of that error, for everything you synthesize, you must declare in the header. So you'll have

@interface FirstViewController : UIViewController {
    IBOutlet MKMapView  *mapView;
    //stores go here!
    //declare store names as Company *cityState
    AddressAnnotation *chiliAuburnAlabama;
    AddressAnnotation *tuttifruttiHomewoodAlabama;

    NSString *website;
    // the rest of your synthesized attributes you want in the view controller;
}
-(IBAction) updateLocation;
-(IBAction) setMap:(id)sender;
-(IBAction) showPin;

@property (nonatomic, retain) NSString *website;
// This is the property that will get rid of your error and you should do it to any other attributes you want to pass into the controller;
@end

You will want that in the annotation header as well if you use it there. It tells that part of the code that it should expect some string to be sent to it and it will know how to handle storing it.

As for calling your GoToMap method, you should set up a delegate for your MoreInfo class when you create it. So in your MoreInfo header, you'll have, among everything else

@interface MoreInfo .... {
    id delegate;
    // everything else;
}
@property (nonatomic, assign) id delegate;
// your methods and other properties;
@end

Then when you create it in your view controller, you'll have

MoreInfo *moreInfoView = [[MoreInfo alloc] initWithNibName:@"MoreInfo" bundle:nil];
moreInfoView.title = view.annotation.title ;
moreInfoView.delegate = self;
// this assigns the current view controller as its delegate;
//moreInfoView.getDirections = [NSURL URLWithString:[NSString stringWithFormat: @"http://maps.google.com/maps?q=%@@%1.6f,%1.6f&z=10", view.annotation.coordinate.latitude, view.annotation.coordinate.longitude]];
moreInfoView.getWebsite = view.annotation.website;
[self.navigationController pushViewController:moreInfoView animated:YES];

Finally, when you want to call GoToWebsite from your MoreInfo, you can call

[self.delegate GoToWebsite];

This of course is assuming that method is in your first view controller (which I could swear it was, but can't find it all the sudden).

But that's basically how you would go about it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!