2011-10-04 82 views
1

我已經搜索,但我沒有找到任何解決方案,我的問題。我想改變已經添加的引腳。這是我的代碼。我在控制檯中看到「Code Here」。註釋標註標題不變

我在.H

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView   *)annotationView; 

myViewController.m

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)annotationView 
{ 
    userPins *myAnnot = (userPins *)annotationView.annotation; 
    [myAnnot setTitle:@"o21"]; 
    NSLog(@"Code here!"); 
} 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
    static NSString *identifier = @"userPins"; 
    if ([annotation isKindOfClass:[userPins class]]) { 

     MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 
     if (annotationView == nil) { 
      annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
     } else { 
      annotationView.annotation = annotation; 
     } 

     annotationView.image = [UIImage imageNamed:@"mapMarker-blue.png"]; 
     annotationView.calloutOffset = CGPointMake(0,0); 

     UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     annotationView.rightCalloutAccessoryView = detailButton; 

     annotationView.enabled = YES; 
     annotationView.canShowCallout = YES; 

     return annotationView; 

    } 

    return nil; 

} 

在userPins.m我有。但我並沒有用setTitle來做這件事。

- (void)setTitle:(NSString *)title { 
    if (_subtitle != subtitle) { 
     [_subtitle release]; 
     _subtitle = [subtitle retain]; 
    } 
} 

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

編輯:@AnnaKarenina

我已經嘗試了所有。但setTitle沒有運行。我要瘋了!這是我的所有代碼。而我沒有得到錯誤。

userpins.m

#import "userPins.h" 

@implementation userPins 
@synthesize fbid = _fbid; 
@synthesize coordinate = _coordinate; 
@synthesize title = _title; 
@synthesize subtitle = _subtitle; 


- (id)initWithfbid:(NSInteger*)fbid 
     coordinate:(CLLocationCoordinate2D)coordinate { 
    if ((self = [super init])) { 
     _fbid = fbid; 
     _coordinate = coordinate; 
    } 
    return self; 
} 


- (NSString *)subtitle{ 
    return @"Anything"; 
} 
- (NSString *)title{ 
    return @"Something"; 
} 

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

@end 

userPins.h

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

@interface userPins : NSObject <MKAnnotation> { 
    NSInteger *_fbid; 
    CLLocationCoordinate2D _coordinate; 
    NSString *_title; 
    NSString *_subtitle; 
} 

@property (nonatomic, readonly) NSInteger *fbid; 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle; 
- (id)initWithfbid:(NSInteger*)fbid coordinate:(CLLocationCoordinate2D)coordinate; 

@end 

viewController.m

#import "lociseViewController.h" 
#import "ASIHTTPRequest.h" 
#import "SBJSON.h" 
#import "userPins.h" 

@implementation lociseViewController 
@synthesize mapView = _mapView; 


- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

// Add new method above refreshTapped 
- (void)plotPositions:(NSString *)responseString { 

    for (id<MKAnnotation> annotation in _mapView.annotations) { 
     [_mapView removeAnnotation:annotation]; 
    } 

    NSDictionary * root = [responseString JSONValue]; 
    NSArray *data = [root objectForKey:@"data"]; 
    for (NSArray * row in data) { 
     NSInteger * fbid = (NSInteger *)[row objectAtIndex:0]; 
     NSNumber * latitude = [row objectAtIndex:1]; 
     NSNumber * longitude = [row objectAtIndex:2]; 

     CLLocationCoordinate2D coordinate; 
     coordinate.latitude = latitude.doubleValue; 
     coordinate.longitude = longitude.doubleValue;    
     userPins *annotation = [[[userPins alloc] initWithfbid:fbid coordinate:coordinate] autorelease]; 
     [_mapView addAnnotation:annotation];   
    } 


} 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
    // 1 
    MKCoordinateRegion mapRegion = [_mapView region]; 
    CLLocationCoordinate2D centerLocation = mapRegion.center; 

    // 2 
    NSString *jsonFile = [[NSBundle mainBundle] pathForResource:@"command" ofType:@"json"]; 

    NSString *formatString = [NSString stringWithContentsOfFile:jsonFile encoding:NSUTF8StringEncoding error:nil]; 
    NSString *json = [NSString stringWithFormat:formatString,      centerLocation.latitude, centerLocation.longitude, 0.5*METERS_PER_MILE]; 


    NSURL *url = [NSURL URLWithString:@"http://localhost/users.json"]; 


    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    request.requestMethod = @"POST"; 
    //request.defaultResponseEncoding = NSISOLatin1StringEncoding; 
    //[request addRequestHeader:@"Content-Encoding" value:@"windows-1255"]; 
    [request addRequestHeader:@"Content-Type" value:@"application/json"]; 
    [request appendPostData:[json dataUsingEncoding:NSUTF8StringEncoding]]; 
    [request setDefaultResponseEncoding:NSUTF8StringEncoding]; 

    // 5 
    [request setDelegate:self]; 
    [request setCompletionBlock:^{   
     NSString *responseString = [request responseString]; 
     NSLog(@"Response: %@", responseString); 
     [self plotPositions:responseString]; 
    }]; 
    [request setFailedBlock:^{ 
     NSError *error = [request error]; 
     NSLog(@"Error: %@", error.localizedDescription); 
    }]; 

    // 6 
    [request startAsynchronous]; 
    //CLLocationManager *locationManager; 
    //locationManager.delegate = self; 

    [super viewDidLoad]; 

} 


- (void)viewWillAppear:(BOOL)animated { 
    // 1 
    CLLocationCoordinate2D zoomLocation; 
    zoomLocation.latitude = 35.333070; 
    zoomLocation.longitude = 33.302875; 

    // 2 
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 50*METERS_PER_MILE, 3*METERS_PER_MILE); 
    // 3 
    MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];     
    // 4 

    [_mapView setRegion:adjustedRegion animated:YES];   
} 

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)annotationView 
{ 
    userPins *myAnnot = (userPins *)annotationView.annotation; 
    [myAnnot setTitle:@"o21"]; 

    NSLog(@"Title: %@", annotationView.annotation.subtitle); 

} 


- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
    static NSString *identifier = @"userPins"; 
    if ([annotation isKindOfClass:[userPins class]]) { 


     MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 
     if (annotationView == nil) { 
      annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
     } else { 
      annotationView.annotation = annotation; 
     } 


     annotationView.image = [UIImage imageNamed:@"mapMarker-blue.png"]; 
     annotationView.calloutOffset = CGPointMake(0,0); 

     UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     annotationView.rightCalloutAccessoryView = detailButton; 


     annotationView.enabled = YES; 
     annotationView.canShowCallout = YES; 

     return annotationView; 

    } 

    return nil; 

} 



- (void)viewDidUnload 
{ 
    [self setMapView:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (void)dealloc { 

    [_mapView release]; 
    [super dealloc]; 
} 
@end 

viewController.h

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

#define METERS_PER_MILE 1609.344 

@interface lociseViewController : UIViewController <MKMapViewDelegate> { 
    MKMapView *_mapView; 
} 

@property (nonatomic, retain) IBOutlet MKMapView *mapView; 
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)annotationView; 


@end 

回答

2

userPins.m中的setTitle:title方法看起來不正確。

setTitle:方法正在查看和設置字幕而不是標題。 title方法返回一個常量字符串,所以即使setTitle:設置了標題ivar,該註釋也會返回相同的常量字符串。

你能解決這些方法或只是聲明titlesubtitlecopy性能和@synthesize他們userPins.m(和釋放的dealloc)。那麼你根本不需要這些方法(它們將自動實現)。

順便說一下,當您添加註釋時,一定要將標題設置爲某個內容(即使是單個空格),否則標註將不會顯示,並且不會調用didSelectAnnotationView

單獨的問題是viewForAnnotation中的內存泄漏。你應該添加一個autorelease你分配的地方+ init annotationView


關於更新的代碼:

在userPins.m,它仍然會返回常量字符串爲subtitletitle這樣就隱藏了改變高德。將其更改爲:

- (id)initWithfbid:(NSInteger*)fbid 
     coordinate:(CLLocationCoordinate2D)coordinate { 
    if ((self = [super init])) { 
     _fbid = fbid; 
     _coordinate = coordinate; 

     //init the ivars here... 
     _title = @"Something"; 
     _subtitle = @"Anything"; 
    } 
    return self; 
} 

/* comment these methods out 
- (NSString *)subtitle{ 
    return @"Anything"; 
} 
- (NSString *)title{ 
    return @"Something"; 
} 
*/ 


一個新的,不相關的問題是,fbid也許應該NSNumber *而不是NSInteger *NSInteger不是一個對象,並且NSNumber是JSON解析器最可能給你的東西。

+0

你真了不起!非常感謝@AnnaKarenina – Okan

+0

但是我在這裏有另一個問題:(標註泡泡顯示不正確。像這樣[link](http://i56.tinypic.com/35arhxs.png)@AnnaKarenina – Okan

+0

它可能是ASIHTTPRequest試圖在選擇註釋的同時從背景更新地圖視圖也許你可以嘗試首先將註解對象集合到一個數組中,而不是爲每個註釋對象調用addAnnotation(單數),然後調用addAnnotations(複數)循環並將其傳遞給數組。 – Anna