在新视图控制器中向注释和打开信息添加披露按钮

Posted

技术标签:

【中文标题】在新视图控制器中向注释和打开信息添加披露按钮【英文标题】:add disclosure button to annotation and open information in new view controller 【发布时间】:2014-04-07 09:44:07 【问题描述】:

我一直在阅读其他一些关于注释中披露按钮的帖子,但我找不到任何非常有用的内容。基本上,我从我从 JSON 解析的位置向地图添加注释。我想为这些注释添加一个披露按钮,当按下披露按钮时,我想在下一个 viewController 中加载特定于该位置的 JSON 信息。到目前为止,这是我的代码,它显示了每个位置的注释。 视图控制器.m

#import "ViewController.h"
#import "Annotation.h"
#import "City.h"
@interface ViewController ()


@end
#define getDatalURL @"http://www.club-hop.com/apptest.php"

@implementation ViewController
@synthesize mapView,jsonArray,citiesArray;


- (void)viewDidLoad

[super viewDidLoad];
[self retrieveData];


City * cityObject;

// load external page into UIWebView
NSMutableArray * locations= [[NSMutableArray alloc]init];
CLLocationCoordinate2D location;
Annotation * myAnn;

for(int u=0; u<citiesArray.count;u++)
cityObject=[citiesArray objectAtIndex:u];
myAnn=[[Annotation alloc]init];
NSNumber *aLat= cityObject.Latitude;
NSNumber *aLon= cityObject.Longitude;

double lat = [aLat doubleValue];
double lon = [aLon doubleValue];

location.latitude= lat;
location.longitude=lon;
myAnn.coordinate = location;
myAnn.title=cityObject.clubName;
myAnn.subtitle=cityObject.cityName;
[locations addObject:myAnn];

[self.mapView addAnnotations:locations];




- (void)didReceiveMemoryWarning

[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.




//class methods
-(void) retrieveData
NSURL * url= [NSURL URLWithString:getDatalURL];
NSData * data= [NSData dataWithContentsOfURL:url];
jsonArray= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

//setup cities array
citiesArray=[[NSMutableArray alloc]init];

for(int i=0; i<jsonArray.count;i++)
    NSString * cID= [[jsonArray objectAtIndex:i] objectForKey:@"id"];
    NSString * cName= [[jsonArray objectAtIndex:i] objectForKey:@"cityName"];
    NSString * cCountry= [[jsonArray objectAtIndex:i] objectForKey:@"cityCountry"];
    NSString * cLine= [[jsonArray objectAtIndex:i] objectForKey:@"clubLine"];
    NSString * clName= [[jsonArray objectAtIndex:i] objectForKey:@"clubName"];
    NSNumber * cLatitude= [[jsonArray objectAtIndex:i] objectForKey:@"Latitude"];
    NSNumber * cLongitude= [[jsonArray objectAtIndex:i] objectForKey:@"Longitude"];

    [citiesArray addObject:[[City alloc]initWithCityName:cName andCityCountry:cCountry      andClubName:clName andClubLine:cLine andLatitude:cLatitude andLongitude:cLongitude andCityId:cID]];




ViewController.h

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

@interface ViewController : UIViewController <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) NSMutableArray * jsonArray;
@property (nonatomic, strong) NSMutableArray * citiesArray;

-(void) retrieveData;


@end

注解.m

#import "Annotation.h"

@implementation Annotation
@synthesize coordinate,title,subtitle;

@end

Annotation.h

#import <Foundation/Foundation.h>
#import <Mapkit/Mapkit.h>
@interface Annotation : NSObject
@property(nonatomic,assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString * title;
@property(nonatomic, copy) NSString * subtitle;

@end

【问题讨论】:

【参考方案1】:

以下链接可能对您有所帮助 MapCallout From apple

下载源代码并检查桥接

【讨论】:

以上是关于在新视图控制器中向注释和打开信息添加披露按钮的主要内容,如果未能解决你的问题,请参考以下文章

使用导航控制器从 mapView 推送详细视图

ios中Mkmapview中的自定义标注

在情节提要中向标签栏控制器场景添加按钮

仅当在表格视图中选择单元格时,详细信息披露才会做出反应

从详细信息披露按钮 segue 传递对象

如何根据按下的按钮取消隐藏标记的详细信息披露按钮