0

我目前正在使用 plist 在地图上放置注释,我已经设法做到这一点,但我现在在尝试从地图视图转到详细视图并从 plist 携带信息时遇到问题。

我已经设置了 plist,因此它在地图视图中具有标题和副标题,然后我设置了一个孩子,这将转到我试图在详细视图中访问的信息。

我已经设法使它进入详细视图,但它不包含任何信息,因为我无法计算出执行此操作所需的代码。

我希望这一切都有意义,因为我自己有点困惑。感谢您对此的任何帮助,您将成为救生员(有点)

这是代码

BrewMapViewController.h

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

@interface BrewMapViewController : UIViewController <MKMapViewDelegate> {
    IBOutlet MKMapView *map;
    NSInteger CurrentLevel;
    NSString *CurrentTitle;
    NSArray *breweries;
}

@property (nonatomic, retain) NSArray *breweries;
@property (nonatomic, readwrite) NSInteger CurrentLevel;
@property (nonatomic, retain) NSString *CurrentTitle;
@end

BrewMapViewController.h

#import "BrewMapViewController.h"
#import "BrewMapAppDelegate.h"
#import "MyAnnotation.h"
#import "ViewController2.h"

@implementation BrewMapViewController

@synthesize breweries, CurrentLevel, CurrentTitle;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *tempArray = [[NSArray alloc] init];
    self.breweries = tempArray;

    BrewMapAppDelegate *AppDelegate = (BrewMapAppDelegate *)[[UIApplication sharedApplication] delegate];

    self.breweries = [AppDelegate.data objectForKey:@"Hull"];
    //breweries = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] 
                                                        // pathForResource:@"test" 
                                                        // ofType:@"xml"]];

    double minLat = [[breweries valueForKeyPath:@"@min.latitude"] doubleValue];
    double maxLat = [[breweries valueForKeyPath:@"@max.latitude"] doubleValue];
    double minLon = [[breweries valueForKeyPath:@"@min.longitude"] doubleValue];
    double maxLon = [[breweries valueForKeyPath:@"@max.longitude"] doubleValue];

    MKCoordinateRegion region;
    region.center.latitude = (maxLat + minLat) / 2.0;
    region.center.longitude = (maxLon + minLon) / 2.0;
    region.span.latitudeDelta = (maxLat - minLat) * 1.05;
    region.span.longitudeDelta = (maxLon - minLon) * 1.05;
    map.region = region;

    for (NSDictionary *breweryDict in breweries){
        MyAnnotation *annotation = [[MyAnnotation alloc] initWithDictionary:breweryDict];
        [map addAnnotation:annotation];

        [annotation release];
    }
    if(CurrentLevel == 0) {
        self.navigationItem.title = @"Map";
    }
    else {
        //self.navigationItem.title = CurrentTitle; 

}

}


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

    if (map.userLocation == annotation){
        return nil;
    }

    NSString *identifier = @"MY_IDENTIFIER";

    MKAnnotationView *annotationView = [map dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil){
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation 
                                                       reuseIdentifier:identifier] 
                          autorelease];
        annotationView.image = [UIImage imageNamed:@"beer.png"];

        annotationView.canShowCallout = YES;
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        annotationView.leftCalloutAccessoryView =  [[[UIImageView  alloc] initWithImage:[UIImage imageNamed:@"pretzel.png"]] autorelease];

    }
    return annotationView;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"tapped");
    ViewController2 *dvController = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:dvController animated:YES];



}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}


- (void)dealloc {
    [breweries release];
    [map release];
    [super dealloc];
}

@end

列表

<plist version="1.0">
<dict>
<key>Hull</key>
    <array>
    <dict>
        <key>name</key>
        <string>Wynkoop Brewery</string>
        <key>address</key>
        <string>1634 18th St., Denver, Co 80902</string>
        <key>latitude</key>
        <real>39.753259</real>
        <key>test</key>
        <string>beer.png</string>
        <key>longitude</key>
        <real>-104.99818</real>
        <key>Children</key>
        <array>
            <dict>
                <key>Title</key>
                <string>Testing</string>
            </dict>
        </array>
    </dict>
    <dict>
        <key>name</key>
        <string>Great Divide Brewing</string>
        <key>address</key>
        <string>2201 Arapahoe Street, Denver, CO 80205</string>
        <key>latitude</key>
        <real>39.753486</real>
        <key>longitude</key>
        <real>-104.988736</real>
        <key>Children</key>
        <array>
            <dict>
                <key>Title</key>
                <string>Testing2</string>
            </dict>
        </array>
    </dict>
    <dict>
        <key>name</key>
        <string>Rock Bottom</string>
        <key>address</key>
        <string>1001 16th Street Denver, CO 80265</string>
        <key>latitude</key>
        <real>39.747186</real>
        <key>longitude</key>
        <real>-104.995037</real>
        <key>Children</key>
        <array>
            <dict>
                <key>Title</key>
                <string>Testing3</string>
            </dict>
        </array>
    </dict>
    <dict>
        <key>name</key>
        <string>Brekenridge Brewery</string>
        <key>address</key>
        <string>471 Kalamath Street, Denver, Colorado 80204</string>
        <key>latitude</key>
        <real>39.723629</real>
        <key>longitude</key>
        <real>-105.000237</real>
        <key>Children</key>
        <array>
            <dict>
                <key>Title</key>
                <string>Testing4</string>
            </dict>
        </array>
    </dict>
</array>

</dict>
</plist>

再次感谢您对此的任何帮助,这将非常有帮助。

4

1 回答 1

0

我假设MyAnnotation该类包含有关您需要的注释的所有信息。
它来自 plist 的事实在这一点上无关紧要。


首先,在 中ViewController2,为注解添加一个属性:

@property (nonatomic, retain) MyAnnotation *annotation; 


接下来,在calloutAccessoryControlTapped委托方法中,设置属性:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"tapped");
    ViewController2 *dvController = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:[NSBundle mainBundle]];

    dvController.annotation = (MyAnnotation *)view.annotation;

    [self.navigationController pushViewController:dvController animated:YES];
}

ViewController2中,您现在可以使用该annotation属性来配置显示。

于 2012-07-31T19:05:06.627 回答