2011-09-03 165 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