注释标注标题不会改变 [英] Annotation callout title does not change

查看:202
本文介绍了注释标注标题不会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜查,但我没有发现任何解决我的问题。我想改变已添加引脚。这里是我的code。我得到code在这里,控制台。

我在.H

   - (空)的MapView:(*的MKMapView)的MapView didSelectAnnotationView:(MKAnnotationView *)annotationView;

myViewController.m

   - (空)的MapView:(*的MKMapView)的MapView didSelectAnnotationView:(MKAnnotationView *)annotationView
{
    userPins * myAnnot =(userPins *)annotationView.annotation;
    [myAnnot的setTitle:@O21];
    的NSLog(@code在这里!);
} - (MKAnnotationView *)的MapView:(*的MKMapView)的MapView viewForAnnotation:(ID< MKAnnotation>){注解
    静态的NSString *标识符= @userPins
    如果([注解isKindOfClass:[userPins类]){        MKPinAnnotationView * annotationView =(MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:符];
        如果(annotationView ==无){
            annotationView = [[MKPinAnnotationView页头] initWithAnnotation:注释reuseIdentifier:符];
        }其他{
            annotationView.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;        返回annotationView;    }    回零;}

在userPins.m我有。但我没有用太多的setTitle做到这一点。

   - (空)的setTitle:(* NSString的)标题{
    如果(_subtitle!=字幕){
        [_subtitle发布]
        _subtitle = [字幕保留]
    }
} - (的NSString *){称号
    返回@奥坎
}

编辑:@AnnaKarenina

我已经尝试了所有。但在的setTitle没有运行。我要疯了!这是我所有的code的。而且我没有得到错误。

userpins.m

 #进口userPins.h@implementation userPins
@synthesize fbid = _fbid;
@synthesize坐标= _coordinate;
@synthesize标题= _title;
@synthesize字幕= _subtitle;
- (ID)initWithfbid:(NSInteger的*)fbid
        坐标:(CLLocationCoordinate2D){坐标
    如果((个体经营= [超级的init])){
        _fbid = fbid;
        _coordinate =协调;
    }
    返回自我;
}
- (的NSString *){字幕
    返回@任何东西;
}
- (的NSString *){称号
    返回@东西;
} - (无效)的dealloc
{
    [_title发布]
    [_subtitle发布]
    [超级的dealloc];
}@结束

userPins.h

 #进口<基金会/ Foundation.h>
#进口< MapKit / MapKit.h>@interface userPins:NSObject的< MKAnnotation> {
    NSInteger的* _fbid;
    CLLocationCoordinate2D _coordinate;
    * NSString的_title;
    * NSString的_subtitle;
}@属性(非原子,只读)NSInteger的* fbid;
@属性(非原子,只读)CLLocationCoordinate2D协调;
@属性(非原子,副本)的NSString *称号;
@属性(非原子,副本)的NSString *字幕;
- (ID)initWithfbid:(NSInteger的*)fbid坐标:(CLLocationCoordinate2D)协调;@结束

viewController.m

 #进口lociseViewController.h
#进口ASIHTT prequest.h
#进口SBJSON.h
#进口userPins.h@implementation lociseViewController
@synthesize图形页面= _mapView;
- (空)didReceiveMemoryWarning
{
    //发布视图,如果它不具有一个上海华。
    [超级didReceiveMemoryWarning];    //释放任何缓存数据,图像等不在使用。
}的#pragma标记 - 查看生命周期//添加上述refreshTapped新方法
- (无效)plotPositions:(* NSString的)responseString {    对于(ID< MKAnnotation>注释中_mapView.annotations){
        [_mapView removeAnnotation:注释]。
    }    *的NSDictionary根= [responseString JSONValue]
    NSArray的*数据= [根objectForKey:@数据];
    对于(数据的NSArray *行){
        NSInteger的* fbid =(NSInteger的*)[排objectAtIndex:0];
        *的NSNumber纬度= [排objectAtIndex:1];
        *的NSNumber经度= [排objectAtIndex:2];        CLLocationCoordinate2D协调;
        coordinate.latitude = latitude.doubleValue;
        coordinate.longitude = longitude.doubleValue;
        userPins *注释= [[[userPins页头] initWithfbid:fbid坐标:坐标]自动释放];
        [_mapView addAnnotation:注释]。
    }
}//实现viewDidLoad中加载的观点,通常从笔尖后做额外的设置。
- (无效)viewDidLoad中
{
    // 1
    MKCoordinateRegion马pregion = [_mapView区];
    CLLocationCoordinate2D centerLocation = MA pregion.center;    // 2
    * NSString的jsonFile = [[一个NSBundle mainBundle] pathForResource:@命令ofType:@JSON];    * NSString的formatString的= [NSString的stringWithContentsOfFile:jsonFile编码:NSUTF8StringEncoding错误:无];
    * NSString的JSON = [的NSString stringWithFormat:formatString中,centerLocation.latitude,centerLocation.longitude,0.5 * METERS_PER_MILE]。
    NSURL * URL = [NSURL URLWithString:@HTTP://localhost/users.json];
    ASIHTT prequest *请求= [ASIHTT prequest requestWithURL:URL]
    request.requestMethod = @POST;
    //request.defaultResponseEncoding = NSISOLatin1StringEncoding;
    // [要求addRequestHeader:@内容编码值:@窗口-1255];
    [要求addRequestHeader:@内容类型值:@应用程序/ JSON];
    [要求appendPostData:[JSON dataUsingEncoding:NSUTF8StringEncoding]];
    [要求setDefaultResponseEncoding:NSUTF8StringEncoding];    // 5
    [要求setDelegate:个体经营];
    [要求setCompletionBlock:^ {
        * NSString的responseString = [要求responseString]
        的NSLog(@回应:%@,responseString);
        [个体经营plotPositions:responseString];
    }];
    [要求setFailedBlock:^ {
        NSError *错误=请求错误]
        的NSLog(@错误:%@,error.localizedDescription);
    }];    // 6
    [请求startAsynchronous]
    // CLLocationManager *的LocationManager;
    //locationManager.delegate =自我;    [超级viewDidLoad中];}
- (无效)viewWillAppear中:(BOOL)动画{
    // 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动画:是];
} - (无效)MapView类:(*的MKMapView)的MapView didSelectAnnotationView:(MKAnnotationView *)annotationView
{
    userPins * myAnnot =(userPins *)annotationView.annotation;
    [myAnnot的setTitle:@O21];    的NSLog(@标题:%@,annotationView.annotation.subtitle);}
- (MKAnnotationView *)的MapView:(*的MKMapView)的MapView viewForAnnotation:(ID< MKAnnotation>){注解
    静态的NSString *标识符= @userPins
    如果([注解isKindOfClass:[userPins类]){
        MKPinAnnotationView * annotationView =(MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:符];
        如果(annotationView ==无){
            annotationView = [[MKPinAnnotationView页头] initWithAnnotation:注释reuseIdentifier:符];
        }其他{
            annotationView.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;        返回annotationView;    }    回零;} - (无效)viewDidUnload
{
    [个体经营setMapView:无];
    [超级viewDidUnload]
    //释放任何保留的主视图的子视图。
    //例如self.myOutlet =零;
} - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    //返回YES支持的方向
    返回(interfaceOrientation == UIInterfaceOrientationPortrait);
} - (无效){的dealloc    [_mapView发布]
    [超级的dealloc];
}
@结束

viewController.h

 #进口<的UIKit / UIKit.h>
#进口< MapKit / MapKit.h>#定义METERS_PER_MILE 1609.344@interface lociseViewController:的UIViewController< MKMapViewDelegate> {
    *的MKMapView _mapView;
}@属性(非原子,保留)IBOutlet中的MKMapView *图形页面;
- (无效)MapView类:(*的MKMapView)的MapView didSelectAnnotationView:(MKAnnotationView *)annotationView;
@结束


解决方案

的setTitle:标题方法userPins.m不看的权利。

的setTitle:方法正在研究和制定,而不是字幕标题。在标题方法返回一个字符串常量所以即使的setTitle:设置标题伊娃,注释将返回相同的常字符串。

您可以修复这些方法或只是声明标题字幕复制属性和 @synthesize 他们userPins.m(在释放的dealloc)。那么你并不需要在所有这些方法(它们会被自动执行)。

顺便说一句,当您添加注释,一定要设置标题的东西(甚至一个空格),否则标注不会显示和 didSelectAnnotationView 不会被调用。

一个单独的问题是 viewForAnnotation 内存泄漏。你应该添加一个自动释放,你的Alloc +的init annotationView

结果
的更新的code:

在userPins.m,它仍然返回字幕常量字符串标题这样就隐藏了改变高德。将其更改为:

   - (ID)initWithfbid:(NSInteger的*)fbid
        坐标:(CLLocationCoordinate2D){坐标
    如果((个体经营= [超级的init])){
        _fbid = fbid;
        _coordinate =协调;        //初始化这里的高德...
        _title = @东西;
        _subtitle = @任何东西;
    }
    返回自我;
}/ *注释这些方法了
- (的NSString *){字幕
    返回@任何东西;
}
- (的NSString *){称号
    返回@东西;
}
* /

结果
一个新的,不相关的问题是, fbid 也许应该是的NSNumber * 而不是 NSInteger的的* NSInteger的不是一个对象和的NSNumber 是JSON解析器是最有可能给你​​。

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

解决方案

The setTitle: and title methods in userPins.m don't look right.

The setTitle: method is looking at and setting subtitle instead of title. The title method returns a constant string so even if setTitle: set the title ivar, the annotation will return the same constant string.

You could fix those methods or just declare title and subtitle as copy 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 an autorelease where you alloc+init annotationView.


Regarding The Updated Code:

In userPins.m, it still returns constant strings for subtitle and title thus hiding the changed ivars. Change it to:

- (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";
}
*/


A new, unrelated problem is that fbid should probably be NSNumber * instead of NSInteger *. NSInteger is not an object and an NSNumber is what the JSON parser is most likely giving you.

这篇关于注释标注标题不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆