使用plist mapView到detailView

时间:2012-07-31 18:38:34

标签: objective-c ios mkmapview mapkit mkannotation

我目前正在使用plist在地图上放置注释,我已设法做到这一点但我现在遇到了从地图视图到详细视图并从plist传递信息的问题。

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

我已设法让它进入详细视图,但它不会获取任何信息,因为我无法计算出所需的代码。

我希望这一切都有意义,因为我自己有点困惑。感谢任何人的帮助,你将成为一个救生员(某种程度)

继承人的代码

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

<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>

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

1 个答案:

答案 0 :(得分: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属性配置显示。