我的应用程序在第一次执行时收到一个json对象(有三个针点位置);有一个mapKit(第一个屏幕)和一个TableView,用户可以在那里查看这些位置。问题是,当我第一次启动应用程序时,地图上没有别针。但是,如果我切换到桌子,我可以看到它们 - 在单元格上 - 如果我再次切换到地图,引脚出现......我不知道为什么会发生这种情况,我不应该看到应用程序启动后立即引脚?地图代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSNotificationCenter *notification=[NSNotificationCenter defaultCenter];
[notification addObserver:self selector:@selector (receiveNotification:) name:@"notification" object:self];
_mapView.showsUserLocation=YES;
_mapView.showsBuildings=YES;
_locationManager = [[CLLocationManager alloc] init];
[_locationManager requestAlwaysAuthorization];
_mapView.delegate = self;
_locationManager.delegate=self;
}
-(void)viewDidAppear:(BOOL)animated{
[self receiveNotification:nil];
}
-(void)receiveNotification:(NSNotification*)notification{
NSArray *spots = [Spot spotType:@"users"];
NSArray *places = [Spot spotWithType:@"users"];
[_mapView addAnnotations:spots];
[_mapView addAnnotations:places];
}
表格:
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource = self;
self.detailList=@[@"Your Favourite Spots",@"Our suggestion"];
}
-(void)viewDidAppear:(BOOL)animated{
_lisbonSpots = [[Spot spotType:@"users"]mutableCopy];
_users=[[Spot spotWithType:@"users"]mutableCopy];
[self.tableView reloadData];
}
编辑 - 竞价类
@implementation Spot
@dynamic ID;
@dynamic name;
@dynamic desc;
@dynamic type;
@dynamic phone;
@dynamic latitude;
@dynamic longitude;
+ (instancetype)spotWithName:(NSString *)name andCoord:
(CLLocationCoordinate2D)coord type:(NSString*)type desc:(NSString*)desc phone:(NSString*)phone{
NSPersistentContainer *persistenceContainer = [AppDelegate sharedDelegate].persistentContainer;
NSManagedObjectContext *context = persistenceContainer.viewContext;
Spot *spot = [NSEntityDescription insertNewObjectForEntityForName:@"Spot" inManagedObjectContext:context];
spot.name = name;
spot.latitude = coord.latitude;
spot.longitude = coord.longitude;
spot.type=type;
spot.desc=desc;
spot.phone=phone;
[[AppDelegate sharedDelegate] saveContext];
return spot;
}
+ (instancetype)spotWithDict:(NSDictionary *)dict {
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([dict[@"latitude"] doubleValue], [dict[@"longitude"] doubleValue]);
return [Spot spotWithName:dict[@"name"] andCoord:coord type:dict[@"type"] desc:dict[@"desc"] phone:dict[@"phone"]];
}
+ (NSArray*)getSpotType:(NSString*)type withPredicate:(NSString*) pred andMessage:(NSString*)message {
NSPersistentContainer *persistenceContainer = [AppDelegate sharedDelegate].persistentContainer;
NSPredicate* predicate = [NSPredicate predicateWithFormat:pred, type];
NSManagedObjectContext *context = persistenceContainer.viewContext;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Spot"];
[request setPredicate:predicate];
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
if (error != nil) {
NSLog(message, [error localizedDescription]);
return nil;
}
return result;
}
+ (NSArray*)spotType:(NSString*)type {
return [Spot getSpotType:type withPredicate:@"type =%@" andMessage:@"[Spot spotType] -> %@"];
}
+ (NSArray*)spotWithType:(NSString*)type {
return [Spot getSpotType:type withPredicate:@"NOT (type = %@)" andMessage:@"[Spot spotWithType] -> %@"];
}
- (CLLocationCoordinate2D)coordinate {
return CLLocationCoordinate2DMake(self.latitude, self.longitude);
}
- (NSString *)title {
return self.name;
}
- (NSString *)description {
return [NSString stringWithFormat:@"%@", self.name];
}
@end
编辑:SpotService类
@implementation SpotService
+ (NSURL *)serviceURL {
return [NSURL URLWithString:@"http://training.reativ.io/ios/lisbon-spots"];
}
+ (BOOL)service:(id<SpotServiceInvoker>)invoker {
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[SpotService serviceURL]];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error != nil) {
NSLog(@"Response: %@", response);
NSLog(@"Error: %@", error);
return;
}
NSArray *lisbonSecrets = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
if ([invoker respondsToSelector:@selector(receiveSpot:)]){
[invoker receiveSpot:lisbonSecrets];
}
for(NSDictionary *dict in lisbonSecrets) {
[Spot spotWithDict:dict];
}
});
}];
[task resume];
return YES;
}
答案 0 :(得分:1)
我的猜测是 - 您的Spot
班级异步检索数据,当您首次从[Spot spotType:@"users"]
viewDidAppear
拨打MapView
时,尚未检索到任何数据。切换视图控制器时,数据显示,一切正常。
但最好向我们展示你的Spot
课程。可能你需要一个completion handler
或类似的东西来实现预期的行为。
此外,每当地图出现在屏幕上时,您都会调用addAnnotations
,这意味着MKMapView
每次调用此方法时都会添加注释的副本。最好添加额外的检查,以确保不会多次添加相同的注释。