0

我有一个 UITableView 可以打开一个带有地图的视图,我称之为 Mapa 类。我在将此表视图中的任何类型的文本信息传递到地图时遇到问题。我需要发送一个字符串文本作为我的地图标题和 CLLocation 的坐标。

这是部分代码

MyTableView.m:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    instMapa = [[Mapa alloc] initWithNibName:@"Mapa" bundle:nil];
    instMapa.mytext = @"pass text to be the title";
    [self.navigationController pushViewController:instMapa animated:YES];

}

映射.h

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

@class MapViewAnnotation;

@interface Mapa : UIViewController <MKMapViewDelegate> {

    IBOutlet MKMapView *mapView;


    NSString *stringTitle;

    NSString *mytext;

    MapViewAnnotation *newAnnotation;



}

@property (nonatomic, retain)   IBOutlet MKMapView *mapView;

@property (nonatomic, retain)   NSString *stringTitle;

@property (nonatomic, retain)   NSString *mytext;

@property (nonatomic, retain)   MapViewAnnotation *newAnnotation;


@end

地图.m

#import "Mapa.h"
#import "MapViewAnnotation.h"


@implementation Mapa

@synthesize mapView;

@synthesize stringTitle;

@synthesize mytext;

@synthesize newAnnotation;




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




    CLLocationCoordinate2D location;


    location.latitude = (double) 51.501468;
    location.longitude = (double) -0.141596;



    // Add the annotation to our map view
    newAnnotation = [[MapViewAnnotation alloc] initWithTitle:mytext andCoordinate:location];
    [self.mapView addAnnotation:newAnnotation];



}

// When a map annotation point is added, zoom to it (1500 range)
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
    MKAnnotationView *annotationView = [views objectAtIndex:0];
    id <MKAnnotation> mp = [annotationView annotation];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1500, 1500);
    [mv setRegion:region animated:YES];
    [mv selectAnnotation:mp animated:YES];
}

// Received memory warning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// If the view unloads, release the map view
- (void)viewDidUnload {

}

- (void)dealloc {

    [newAnnotation release];

    [mytext release];

    [stringTitle release];

    [mapView release];
    [super dealloc];
}


@end

MapViewAnnotation.h

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


@interface MapViewAnnotation : NSObject <MKAnnotation> {

    NSString *title;
    CLLocationCoordinate2D coordinate;

}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;


@end

MapViewAnnotation.m

@implementation MapViewAnnotation

@synthesize title, coordinate;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
    [super init];
    title = ttl;
    coordinate = c2d;
    return self;
}

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


@end

谢谢您的帮助!

4

1 回答 1

0

从你的问题,我明白了;您需要将所选表格视图单元格的字符串值传递给您的地图视图。为此,您需要在您的didSelectRowAtIndexPath,

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
 instMapa.mytext = cell.textLabel.text;
于 2012-07-04T18:27:02.637 回答