注释标注标题不变
我已经搜索过,但没有找到任何解决我的问题的方法。我想更改已添加的引脚。这是我的代码。我在控制台中收到“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.mi 中有。但我也没有用 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
I have searched but i didn't found any solution to my problem. I want to change already added pin. And here is my code. I'm getting "Code Here" in console.
I have in .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;
}
In userPins.m i have. But i didn't do this with setTitle too.
- (void)setTitle:(NSString *)title {
if (_subtitle != subtitle) {
[_subtitle release];
_subtitle = [subtitle retain];
}
}
- (NSString *)title{
return @"okan";
}
Edit: @AnnaKarenina
I have tried all. But setTitle is not running. I'm going crazy! This is all of my code. And I'm not getting errors.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
userPins.m 中的
setTitle:
和title
方法看起来不正确。setTitle:
方法查看并设置副标题而不是标题。title
方法返回一个常量字符串,因此即使setTitle:
设置标题 ivar,注释也会返回相同的常量字符串。您可以修复这些方法,或者仅将
title
和subtitle
声明为copy
属性,并在 userPins.m 中将它们声明为@synthesize
(并在 dealloc 中释放)。那么你就根本不需要这些方法(它们会自动实现)。顺便说一句,当您添加注释时,请务必将标题设置为某些内容(即使是一个空格),否则标注将不会显示,并且
didSelectAnnotationView
将不会被调用。另一个问题是 viewForAnnotation 中的内存泄漏。您应该在 alloc+init
annotationView
位置添加一个autorelease
。关于更新的代码:
在userPins.m中,它仍然返回
subtitle
和title
的常量字符串,从而隐藏了更改的ivars。将其更改为:一个新的、不相关的问题是
fbid
可能应该是NSNumber *
而不是NSInteger *
。NSInteger
不是一个对象,而NSNumber
是 JSON 解析器最有可能为您提供的内容。The
setTitle:
andtitle
methods in userPins.m don't look right.The
setTitle:
method is looking at and setting subtitle instead of title. Thetitle
method returns a constant string so even ifsetTitle:
set the title ivar, the annotation will return the same constant string.You could fix those methods or just declare
title
andsubtitle
ascopy
properties and@synthesize
them in userPins.m (and release in dealloc). Then you don't need those methods at all (they will be implemented automatically).By the way, when you add the annotation, be sure to set the title to something (even a single space) otherwise the callout will not display and
didSelectAnnotationView
will not be called.A separate problem is a memory leak in
viewForAnnotation
. You should add anautorelease
where you alloc+initannotationView
.Regarding The Updated Code:
In userPins.m, it still returns constant strings for
subtitle
andtitle
thus hiding the changed ivars. Change it to:A new, unrelated problem is that
fbid
should probably beNSNumber *
instead ofNSInteger *
.NSInteger
is not an object and anNSNumber
is what the JSON parser is most likely giving you.