2011-09-03 223 views
0

我有一个叫做“CortesViewController”的ViewController,它有一个名为myMap的MKMapView。我有一个函数showAddress,它在CortesViewController.h中定义并在相应的.m文件中实现。从另一个ViewController类调用函数

- (void) showAddress:(float) lat :(float) lon :(int) keytype 
{ 
centerCoordinate.latitude = lat; 
    centerCoordinate.longitude = lon ; 
     NSLog(@"with key = 0, lat lon are %f, %f", centerCoordinate.latitude, centerCoordinate.longitude); 
    [mymap setCenterCoordinate:centerCoordinate animated:TRUE] ; 
} 

我有其他的UITableViewController“PlacesViewController”,其中包含与名称和经纬度位置列表,并可以通过放置在CortesViewController按钮被带到前面。点击任何地点的名称时,我想返回到mymap,在地图中心显示选定的地点。所以我称之为“showAddress”功能

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

in PlaceViewController.m。实现如下所示。

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

    Place *placeAtIndex = (Place *)[appDelegate.PlacesArray objectAtIndex:indexPath.row]; 

    NSLog(@"required lat long is %f, %f ", placeAtIndex.PlaceLatitude, placeAtIndex.PlaceLongitude); 


    CortesViewController *returnToMap = [[CortesViewController alloc] init]; 



    float tableLatitude = placeAtIndex.PlaceLatitude ; 
    float tableLongitude = placeAtIndex.PlaceLongitude; 
    [returnToMap showAddress :tableLatitude :tableLongitude : 0]; 

    [self.navigationController dismissModalViewControllerAnimated:YES]; 
    } 

代码运行而不会MyMap中出现错误或警告,但鉴于尽管点击具有不同的纬度和经度的地方不会改变。 showAddress将输入值lat,lon正确地存储在PlaceViewController.m中的UITableView中。但线

[mymap setCenterCoordinate:centerCoordinate animated:TRUE] ;

似乎没有从

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath调用时工作。

请帮忙。感谢您提前提供任何帮助。

回答

1

看来你正在改变在这一行

[returnToMap showAddress :tableLatitude :tableLongitude : 0];

一些值如果showAddress你改变观点也可能比它可能不反映在某些情况下的变化(读主题和UI组件的细节在iOS中进行交互)。

所以,我会建议你只需要改变变量showAddress和CortesViewController

的viewWillAppear中的方法相应地应用在视图中进行更改

如果上面没有适用于你的情况下,然后张贴在这里,这样我可以详细地了解问题。

+0

我没有改变viewWillAppear中的方法,它完美地工作。感谢您的回答。 – alekhine

0

didSelectRowAtIndexPath中,您正在创建CortesViewController的新实例,该实例与呈现PlaceViewController的实例无关。

呈现时,你应该通过CortesViewController的参考PlaceViewController,或者您可以使用NSNotificationCenter将消息发送到CortesViewController,或(可能是最好的)使用委托+协议消息发回CortesViewController

此外,不是你的问题,但showAddress的定义不遵循约定。您没有命名参数。相反的:

- (void) showAddress:(float) lat :(float) lon :(int) keytype 

我建议:

- (void) showAddressWithLat:(float)lat Lon:(float) lon KeyType:(int) keytype 

,然后你会这样称呼它:

[returnToMap showAddressWithLat:tableLatitude Lon:tableLongitude KeyType:0]; 
+0

谢谢你的建议安娜。您将CortesViewController的引用传递给PlaceViewController的建议会引导我解决问题。我按照你的建议修改了函数名称。 – alekhine