当我们导航到MapTabBar时重新加载地图

时间:2012-11-15 15:12:18

标签: iphone objective-c ios

我有一个tabBarView控制器和一个TableViewController。当我点击一行时,我想去另一个带有Map的UIViewController的Tab:

- (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];
}

当我再次访问该页面时,问题是Map没有得到更新。当我用PushSegue尝试它时,它可以工作。但是我如何才能使用TabBar呢?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

iPhone: How to Pass Data Between Several Viewcontrollers in a Tabbar App

Passing a managedObjectContext through to a UITabBarController's views

this tutorial也很有用。