2012-06-28 1 views
0

배열에서 내지도 주석의 버튼으로 전화 객체를 호출하려면 어떻게해야합니까?JSON 맵 주석의 문자열을 iOS의 버튼 호출에 전달하는 방법은 무엇입니까?

JSON 배열에서로드하는 핀을 가지고 있으며 전화 번호에서 주소를 파싱합니다. 전화가 푸시 될 때 버튼에 올바른 전화 번호를 얻으려면 어떻게해야합니까?

 for(id key in json) { 
      id value = [json objectForKey:key]; 
      NSString *titlePin = [value valueForKey:@"address"]; 
      NSString *addressPhone = [value valueForKey:@"title"]; 
      NSString *latitude = [value valueForKey:@"latitude"]; 
      NSString *longitude = [value valueForKey:@"longitude"]; 

      NSArray* foo = [addressPhone componentsSeparatedByString: @":"]; 
      NSString* justAddress = [foo objectAtIndex: 0]; 
      NSString* phone = [foo objectAtIndex: 1]; 

      double myLatitude = [latitude doubleValue]; 
      double myLongitude = [longitude doubleValue]; 

      MKCoordinateRegion location1; 
      location1.center.latitude =myLatitude; 
      location1.center.longitude= myLongitude; 
      location1.span.longitudeDelta=0.1; 
      location1.span.latitudeDelta =0.1; 

      MapAnnotation *ann1 =[[[MapAnnotation alloc] init] autorelease]; 
      ann1.title=[NSString stringWithFormat:@"%@",titlePin]; 
      ann1.subtitle=[NSString stringWithFormat:@"%@",justAddress]; 
//EDIT added this line for part of sollution 
    ann1.phone=[NSString stringWithFormat:@"%@",phone]; 
      ann1.coordinate= location1.center; 
      [mapView addAnnotation:ann1]; 
     } 
    } 
} 

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 
    MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"]; 
    MyPin.pinColor = MKPinAnnotationColorPurple; 

    UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [advertButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside]; 

    MyPin.rightCalloutAccessoryView = advertButton; 
    MyPin.draggable = NO; 
    MyPin.highlighted = YES; 
    MyPin.animatesDrop=TRUE; 
    MyPin.canShowCallout = YES; 

    return MyPin; 
} 

-(void)button:(id)sender { 

    // Call the phone number here!!!!!!! 
    //NSLog(@"Button action: %@",phone); 

} 

MapAnnotaion.h 사과 샘플 코드에서 코드의

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

@interface MapAnnotation : NSObject <MKAnnotation> 
{  
    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
    NSString *phone; 

} 

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle; 
@property (nonatomic, copy) NSString *phone; 

@end 

MapAnnotaion.m

#import "MapAnnotation.h" 

@implementation MapAnnotation 

@synthesize coordinate, title, subtitle, phone; 

-(void)dealloc 
{ 
    [title release]; 
    [subtitle release]; 
    [phone release]; 
    [super release]; 
    [super dealloc]; 
} 

@end 
+0

하위 클래스에 보여줍니다 알림 –

답변

0

부 : MapCallouts

SFAnnotation.h

#import <MapKit/MapKit.h> 

@interface SFAnnotation : NSObject <MKAnnotation> 
{ 
    UIImage *image; 
    NSNumber *latitude; 
    NSNumber *longitude; 
} 

@property (nonatomic, retain) UIImage *image; 
@property (nonatomic, retain) NSNumber *latitude; 
@property (nonatomic, retain) NSNumber *longitude; 

@end 

는 SFAnnotation.m 제목 문자열이

#import "SFAnnotation.h" 

@implementation SFAnnotation 

@synthesize image; 
@synthesize latitude; 
@synthesize longitude; 


- (CLLocationCoordinate2D)coordinate; 
{ 
    CLLocationCoordinate2D theCoordinate; 
    theCoordinate.latitude = 37.786996; 
    theCoordinate.longitude = -122.419281; 
    return theCoordinate; 
} 

- (void)dealloc 
{ 
    [image release]; 
    [super dealloc]; 
} 

- (NSString *)title 
{ 
    return @"San Francisco"; 
} 

// optional 
- (NSString *)subtitle 
{ 
    return @"Founded: June 29, 1776"; 
} 

@end 

통화 밖으로의 제목

편집 한

-(void)button:(id)sender { 

UIButton *button = (UIButton *)sender; 

MKAnnotationView *annotationView = button.superview.superview; 

MapAnnotaion *mapAnnotaion = annotationView.annotation; 

NSLog(@"%@", mapAnnotation.phone); 


// Call the phone number here!!!!!!! 
//NSLog(@"Button action: %@",phone); 

} 당신이를

+0

내가 틀린 것을 말하고 있을지도 모른다. 제목과 부제목을 잘 설정할 수 있습니다. 내가 할 수없는 일은 주석을 위해 전화를 가져 와서 - (void) 버튼에 전달하는 것입니다. (id) 버튼을 눌렀을 때 호출이 열려있는 핀의 발신자 메소드입니다. – Rick

+0

@Rick 호출 버튼을 눌렀을 때 작업을 호출 하시겠습니까? –

+0

예, 그렇습니다. "문제가없는"버튼을 눌렀을 때 기록 할 수 있습니다. 올바른 배열 객체의 "phone"문자열을 기록하려면 어떻게합니까? – Rick

관련 문제