IOS:如何在UIMapView上显示标签文本值

时间:2015-11-20 10:38:43

标签: ios objective-c uitableview google-maps uiviewcontroller

我正在处理UITableView,其中必须通过StoryBoard将数据从一个视图传递到另一个视图。 数据现在正在通过故事板 现在我想在第二个ViewController上传递的数据显示在UIMapView

我正在分享我的代码 第一个VC的.h文件

{
    UITableView * addresstable;
    NSArray * addresses;


    NSString *localStringValue;
}

第一个VC的.m文件

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

    NSUInteger row = [indexPath row];
    NSString *value = [addresses objectAtIndex:row];

    localStringValue = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
    NSLog(@"ID is Here: %@", value);



    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    detailmapViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"detailmapViewController"];
    [self presentModalViewController:vc animated:YES];


    vc.PassID=value; ///assign the value after presenting
}
第二个视图的

.h文件

{
    UILabel *addresslbl;

    NSString *PassID;
}

@property(nonatomic,retain)IBOutlet UILabel* addresslbl;
@property (weak, nonatomic) IBOutlet MKMapView *mapvw;
@property (strong, nonatomic) IBOutlet CLLocationManager *locationManger;


@property (nonatomic, retain) NSString *PassID;

第二个视图的.m文件

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    addresslbl.text=PassID;
    NSLog(@"Label is Here %@",self.PassID);

}

我还希望在MAP上显示第二个VC标签的值,因为我使用了所有框架和委托文件 但我不知道如何调用地图的标签文字值 因为我使用以下代码

self.mapvw.delegate=self;

    NSString *location =  //---> how to put label value here <---

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:location
                 completionHandler:^(NSArray* placemarks, NSError* error){
                     if (placemarks && placemarks.count > 0) {
                         CLPlacemark *topResult = [placemarks objectAtIndex:0];
                         MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];

                         MKCoordinateRegion region = self.mapvw.region;
                         region.center = [(CLCircularRegion *)placemark.region center]; //placemark.region.center;
                         region.span.longitudeDelta /= 1800.0;
                         region.span.latitudeDelta /= 1800.0;

                         [self.mapvw setRegion:region animated:YES];
                         [self.mapvw addAnnotation:placemark];
                     }
                 }
     ];

1 个答案:

答案 0 :(得分:1)

问题是,您要在显示viewcontroller之后的值分配值。所以,它不会得到价值。

试试这个

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    detailmapViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"detailmapViewController"];

vc.PassID=value;

[self presentModalViewController:vc animated:YES];

对于您的第二个问题,为什么不简单地将PassID值直接分配给location字符串

NSString *location = self.PassID;

NSString *location = addresslbl.text;