2011-09-03 5 views
0

"CortesViewController"라는 ViewController가 있으며 MymapView 호출 된 mymap이 있습니다. CortesViewController.h에 정의되어 있고 해당 .m 파일에 구현 된 함수 showAddress가 있습니다.다른 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] ; 
} 

나는 이름과 위도 및 경도와 장소의 목록을 포함하고 CortesViewController에 배치 버튼을 앞으로 가져 수있는 다른있는 UITableViewController "PlacesViewController"가. 어떤 장소의 이름을 클릭하면지도의 중심에서 선택된 장소를 표시하는 mymap으로 돌아가고 싶습니다. 그래서 "showAddress"함수라고 부릅니다.

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

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

코드는 서로 다른 위도와 경도가있는 곳을 클릭에도 불구하고 변하지 않는 내지도에서 오류 또는 경고 만보기없이 실행됩니다. showAddress는 PlaceViewController.m의 UITableView에 저장된 입력 값 lat, lon을 올바르게 사용합니다. 그러나

[mymap setCenterCoordinate:centerCoordinate animated:TRUE] ;

해달라고 라인은

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath에서 호출 할 때 작동하는 것.

도와주세요. 사전에 도움을 주셔서 감사합니다.

답변

1

이 showAddress에 당신이 (어떤 경우에는 변경 사항을 반영 스레드와 UI 구성 요소의 내용을 읽어하지 않을 수도 아마보다보기를 변경하는 경우이 라인

[returnToMap showAddress :tableLatitude :tableLongitude : 0];

에서 일부 값을 변경하는 것 같다 iOS에서의 상호 작용).

그래서, 난 그냥 showAddress에서 변수를 변경 이상 내가 구체적으로 문제를 이해할 수 있도록 경우 여기에 게시 당신에 적용 할 수없는 경우 CortesViewController

의 viewWillAppear 방법에 따라

을보기에서 변경 사항을 적용 제안합니다.

+0

나는 ViewWillAppear 방법을 변경 않았고 완벽하게 일했다. 귀하의 답변에 감사드립니다. – alekhine

0

didSelectRowAtIndexPath에는 CortesViewController이라는 새 인스턴스가 생성되며이 인스턴스는 PlaceViewController이라는 인스턴스와는 아무런 관련이 없습니다. 를 제시 할 때

당신은 PlaceViewControllerCortesViewController의 참조를 전달해야하거나 CortesViewController에게 메시지를 보내 NSNotificationCenter를 사용하거나 (아마도 최고의) CortesViewController 다시 메시지에 위임 + 프로토콜을 사용합니다.

또한 문제가 아니지만 showAddress의 정의는 규칙을 따르지 않습니다. 명명 된 매개 변수가 없습니다.대신에 :

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

을 그리고 당신은 다음과 같이 호출 것 :

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

내가 제안

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

제안 해 주셔서 고맙습니다. CortesViewController의 참조를 PlaceViewController로 전달하라는 제안은이 문제를 해결할 수있게 해줍니다. 내가 제안한대로 함수 이름을 변경했습니다. – alekhine

관련 문제