2012-10-24 8 views
0

사용자가 핀을 누를 때 눌러주는 mapView가 있습니다. 한 번에 여러 개의 핀이있을 수 있으며 각 주석 뷰에는 눌려 질 때 새 뷰를 스택에 푸시하는 콜 아웃이 있습니다. 내가하고 싶은 일은 주석보기의 제목을 두 번째보기의 레이블로 전달하는 것입니다. 나는 두 번째보기를 호출하는 코드를 여기에콜 아웃이있는 여러 주석이있는 MapView, 다음 뷰에 제목을 전달하는 방법

-(void)press:(UILongPressGestureRecognizer *)recognizer 
{ 
    CGPoint touchPoint = [recognizer locationInView:worldView]; 
    CLLocationCoordinate2D touchMapCoordinate = [worldView convertPoint:touchPoint toCoordinateFromView:worldView]; 

    geocoder = [[CLGeocoder alloc]init]; 
    CLLocation *location = [[CLLocation alloc]initWithCoordinate:touchMapCoordinate 
                altitude:CLLocationDistanceMax 
              horizontalAccuracy:kCLLocationAccuracyBest 
              verticalAccuracy:kCLLocationAccuracyBest 
                timestamp:[NSDate date]]; 
    [geocoder reverseGeocodeLocation:location 
       completionHandler:^(NSArray *placemarks, NSError *error) { 
        NSLog(@"reverseGeocoder:completionHandler: called"); 
        if (error) { 
         NSLog(@"Geocoder failed with error: %@", error); 
        } else { 
         CLPlacemark *place = [placemarks objectAtIndex:0]; 
         address = [NSString stringWithFormat:@"%@ %@, %@ %@", [place subThoroughfare], [place thoroughfare], [place locality], [place administrativeArea]]; 

         if (UIGestureRecognizerStateBegan == [recognizer state]) { 
          addressPin = [[MapPoint alloc]initWithCoordinate:touchMapCoordinate 
                     title:address]; 
          [worldView addAnnotation:addressPin]; 
         } 
        } 
       }]; 
} 

: 그리고 내가 필요로 할 때 보통

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    PinViewController *pinViewController = [[PinViewController alloc]init]; 
    [self passValues]; 
    [[self navigationController]pushViewController:pinViewController animated:YES]; 
} 

답변

1

당신은 MKAnnotation을 대체 할 수 있습니다 (예를 들어 MyLocation 용) 및 MyLocation.h 파일에 선언

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 

@interface MyLocation : NSObject <MKAnnotation> { 
    NSNumber *identyfier; 
    NSString *_name; 
    NSString *_address; 
    CLLocationCoordinate2D _coordinate; 
} 

@property (copy) NSString *name; 
@property (copy) NSString *address; 
@property (copy) NSNumber *identyfier; 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

- (id)initWithName:(NSString*)name address:(NSString*)address 
    coordinate:(CLLocationCoordinate2D)coordinate identyfier:(NSNumber *) identyfier; 
MyLocation.m 파일

@end

을 : 맵보기에서

#import "MyLocation.h" 

@implementation MyLocation 

@synthesize name = _name; 
@synthesize address = _address; 
@synthesize coordinate = _coordinate; 
@synthesize identyfier = _identyfier; 

- (id)initWithName:(NSString*)name address:(NSString*)address 
    coordinate:(CLLocationCoordinate2D)coordinate identyfier:(NSNumber *)identyfier { 
    if ((self = [super init])) { 
     _name = [name copy]; 
     _address = [address copy]; 
     _coordinate = coordinate; 
     _identyfier = identyfier; 
    } 
    return self; 
} 

당신이 주석이 Methos는 사용 선언지도 위임 예를 들면 그래서

MyLocation *pin = [[MyLocation alloc] initWithName:place.name address:place.address coordinate:coordinate2D identyfier:some_id]; 

을 :

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 

다음을 사용할 수 있습니다.

((MyLocation *)annotation).identyfier 

+0

가 다운 될 것 핀의 유한 수없는, 그래서 어떻게 핀을 임의의 NSNumber 식별자를 제공 할 (물론 당신이 MyLocation 클래스에서 다른 변수를 사용할 수 있습니다) 선택 주석을 확인? –

+0

내 예제에서는 맵에 create 및 next show annotations에 sqlite를 사용합니다. 나는 거기에 "자동 증가"와 ID가, 그래서 거기에 어떤 핀이 같은 ID를 가지고 방법이 없습니다. 주석을 배열에 저장하면 배열의 인덱스를 사용할 수 있습니다. – edzio27

+0

그래서 이해가 안되는 것은 MyLocation 클래스의 주소 변수에 지오 코딩 된 주소를 저장하는 방법입니다. 너무 많은 질문을하는 것에 불쌍하지만 미안합니다. –

0

난 그냥 만들 변수를 전달하려면 여기를

내가 핀을 드롭 코드 두보기 모두에서 읽을 수있는 전역 변수.

통근 당신은 .H 파일에 넣고하는 .m 파일에서 당신은 세계적으로 some_variable_name를 넣어 (있는 NSString *) (있는 NSString *) some_variable_name,있다;

은 모든 뷰

또는 다음하는 .m 파일의 상단에있는 글로벌 설정보기를 (포함 모든보기에서 읽을 수있는 변수 당신 앞에있는 + 기호를 읽을 수있는 그리고 령 어에 의해 .H 파일에

뭔가 = [someview that_variable]