2

我有一个 tabBarView 控制器和一个 TableViewController。当我单击一行时,我想转到另一个选项卡,即带有地图的 UIViewController:

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

- (void) setMap {

    NSLog(@"Name:%@\n", storeName);
    NSLog(@"Adress:%@\n", storeAddress);



        self.indexMapView.delegate = self;

        self.indexMapView.showsUserLocation = NO;

        self.locationManager = [[CLLocationManager alloc] init];
        [locationManager setDelegate:self];

        [locationManager setDistanceFilter:kCLDistanceFilterNone];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

        [self.indexMapView setShowsUserLocation:YES];

        CLLocationCoordinate2D location = [self getLocationFromAddressString:self.storeAddress];

        NSLog(@"%g", location.latitude);

        MapViewAnnotation *mapAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Store location" coordinate:location];
        [self.indexMapView addAnnotation:mapAnnotation];

}

-(CLLocationCoordinate2D) getLocationFromAddressString:(NSString*) addressStr {

    NSLog(@"FFF");

    NSString *urlStr = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
                        [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSError *error = nil;

    NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr] encoding:NSUTF8StringEncoding error:&error];

    NSArray *items = [locationStr componentsSeparatedByString:@","];

    double lat = 0.0;
    double lon = 0.0;

    if([items count] >= 4 && [[items objectAtIndex:0] isEqualToString:@"200"]) {
        lat = [[items objectAtIndex:2] doubleValue];
        lon = [[items objectAtIndex:3] doubleValue];
    }
    else {
        NSLog(@"Address, %@ not found: Error %@",addressStr, [items objectAtIndex:0]);
    }

    CLLocationCoordinate2D location;
    location.latitude = lat;
    location.longitude = lon;

    return location;
}

这就是我去 MapViewController 的方式:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MapViewController *mapViewController = [[MapViewController alloc]init];

    [mapViewController setStoreAddress:store.address];
    [mapViewController setStoreName:store.name];
    [mapViewController viewDidLoad];

    [self.tabBarController setSelectedIndex:4];
}

问题是当我再次访问该页面时地图没有更新。当我尝试使用它时PushSegue,它可以工作。但我怎样才能使它与 TabBar 一起工作?

4

2 回答 2

0

好吧,看起来您注释掉了对 的调用-setMap,这意味着当视图加载时,不会发生任何感兴趣的事情。那是故意的吗?

于 2012-11-15T15:14:23.277 回答
0

我想出了如何通过标签栏传递数据。您可以在这些链接中找到更多描述:

iPhone:如何在标签栏应用程序中的多个视图控制器之间传递数据

将 managedObjectContext 传递给 UITabBarController 的视图

本教程也很有用。

于 2012-11-16T22:26:50.547 回答