Skip to content

Commit cd7a018

Browse files
committed
Added Lecture 15 code: more MapKit + container views
1 parent 5be61d3 commit cd7a018

8 files changed

+301
-8
lines changed

Photomania/Photomania.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
BC6CE81716F0B82C0050D053 /* Photographer.m in Sources */ = {isa = PBXBuildFile; fileRef = BC6CE81616F0B82C0050D053 /* Photographer.m */; };
4141
BC6CE81A16F0B82C0050D053 /* Photo.m in Sources */ = {isa = PBXBuildFile; fileRef = BC6CE81916F0B82C0050D053 /* Photo.m */; };
4242
BC6CE81C16F0BE5A0050D053 /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC6CE81B16F0BE5A0050D053 /* MapKit.framework */; };
43+
BCC7F73D16F33FF500FFF003 /* PhotoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = BCC7F73C16F33FF500FFF003 /* PhotoViewController.m */; };
4344
/* End PBXBuildFile section */
4445

4546
/* Begin PBXFileReference section */
@@ -101,6 +102,8 @@
101102
BC6CE81816F0B82C0050D053 /* Photo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Photo.h; sourceTree = "<group>"; };
102103
BC6CE81916F0B82C0050D053 /* Photo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Photo.m; sourceTree = "<group>"; };
103104
BC6CE81B16F0BE5A0050D053 /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; };
105+
BCC7F73B16F33FF500FFF003 /* PhotoViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PhotoViewController.h; sourceTree = "<group>"; };
106+
BCC7F73C16F33FF500FFF003 /* PhotoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PhotoViewController.m; sourceTree = "<group>"; };
104107
/* End PBXFileReference section */
105108

106109
/* Begin PBXFrameworksBuildPhase section */
@@ -165,6 +168,8 @@
165168
BC3E9BCD16EDEF1D00103EE4 /* DemoPhotographerCDTVC.m */,
166169
BC3E9BBD16EDE70400103EE4 /* ImageViewController.h */,
167170
BC3E9BBE16EDE70400103EE4 /* ImageViewController.m */,
171+
BCC7F73B16F33FF500FFF003 /* PhotoViewController.h */,
172+
BCC7F73C16F33FF500FFF003 /* PhotoViewController.m */,
168173
BC3E9BC016EDE76200103EE4 /* AttributedStringViewController.h */,
169174
BC3E9BC116EDE76200103EE4 /* AttributedStringViewController.m */,
170175
BC3E9BC316EDE77000103EE4 /* CoreDataTableViewController.h */,
@@ -320,6 +325,7 @@
320325
BC6CE80E16F0B7620050D053 /* Photo+MKAnnotation.m in Sources */,
321326
BC6CE81716F0B82C0050D053 /* Photographer.m in Sources */,
322327
BC6CE81A16F0B82C0050D053 /* Photo.m in Sources */,
328+
BCC7F73D16F33FF500FFF003 /* PhotoViewController.m in Sources */,
323329
);
324330
runOnlyForDeploymentPostprocessing = 0;
325331
};

Photomania/Photomania/ImageViewController.m

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ - (void)setImageURL:(NSURL *)imageURL
4242

4343
- (void)resetImage
4444
{
45+
NSLog(@"Loading image from %@...", self.imageURL);
4546
if (self.scrollView) {
4647
self.scrollView.contentSize = CGSizeZero;
4748
self.imageView.image = nil;

Photomania/Photomania/MapViewController.m

+44-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#import "MapViewController.h"
1010

1111
@interface MapViewController ()
12-
12+
@property (nonatomic) BOOL needUpdateRegion;
1313
@end
1414

1515
@implementation MapViewController
@@ -18,6 +18,16 @@ -(void)viewDidLoad
1818
{
1919
[super viewDidLoad];
2020
self.mapView.delegate = self;
21+
self.needUpdateRegion = YES;
22+
}
23+
24+
-(void)viewDidAppear:(BOOL)animated
25+
{
26+
[super viewDidAppear:animated];
27+
if(self.needUpdateRegion)
28+
{
29+
[self updateRegion];
30+
}
2131
}
2232

2333
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
@@ -55,4 +65,37 @@ -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)
5565
}
5666
}
5767

68+
-(void)updateRegion
69+
{
70+
self.needUpdateRegion = NO;
71+
CGRect boundingRect;
72+
BOOL started = NO;
73+
for(id <MKAnnotation> annotation in self.mapView.annotations)
74+
{
75+
CGRect annotationRect = CGRectMake(annotation.coordinate.latitude, annotation.coordinate.longitude, 0, 0);
76+
if(!started)
77+
{
78+
started = YES;
79+
boundingRect = annotationRect;
80+
}
81+
else
82+
{
83+
boundingRect = CGRectUnion(boundingRect, annotationRect);
84+
}
85+
}
86+
if(started)
87+
{
88+
boundingRect = CGRectInset(boundingRect, -0.2, -0.2);
89+
if((boundingRect.size.width < 20) && (boundingRect.size.height < 20))
90+
{
91+
MKCoordinateRegion region;
92+
region.center.latitude = boundingRect.origin.x + (boundingRect.size.width / 2);
93+
region.center.longitude = boundingRect.origin.y + (boundingRect.size.height / 2);
94+
region.span.latitudeDelta = boundingRect.size.width;
95+
region.span.longitudeDelta = boundingRect.size.height;
96+
[self.mapView setRegion:region animated:YES];
97+
}
98+
}
99+
}
100+
58101
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// PhotoViewController.h
3+
// Photomania
4+
//
5+
// Created by Vasco Orey on 3/15/13.
6+
// Copyright (c) 2013 Delta Dog Studios. All rights reserved.
7+
//
8+
9+
#import "ImageViewController.h"
10+
#import "Photo.h"
11+
12+
@interface PhotoViewController : ImageViewController
13+
14+
@property (nonatomic, strong) Photo *photo;
15+
16+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// PhotoViewController.m
3+
// Photomania
4+
//
5+
// Created by Vasco Orey on 3/15/13.
6+
// Copyright (c) 2013 Delta Dog Studios. All rights reserved.
7+
//
8+
9+
#import "PhotoViewController.h"
10+
#import "MapViewController.h"
11+
#import "Photo+MKAnnotation.h"
12+
13+
@interface PhotoViewController ()
14+
@property (nonatomic, strong) MapViewController *mapVC;
15+
@end
16+
17+
@implementation PhotoViewController
18+
19+
-(void)setPhoto:(Photo *)photo
20+
{
21+
_photo = photo;
22+
self.title = photo.title;
23+
self.imageURL = [NSURL URLWithString:photo.imageURL];
24+
}
25+
26+
-(void)viewDidLoad
27+
{
28+
[super viewDidLoad];
29+
[self.mapVC.mapView addAnnotation:self.photo];
30+
}
31+
32+
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
33+
{
34+
if([segue.identifier isEqualToString:@"setMapVC:"])
35+
{
36+
if([segue.destinationViewController isKindOfClass:[MapViewController class]])
37+
{
38+
self.mapVC = (MapViewController *)segue.destinationViewController;
39+
}
40+
}
41+
}
42+
43+
@end

Photomania/Photomania/PhotographerMapViewController.m

+26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import "PhotographerMapViewController.h"
1010
#import <CoreData/CoreData.h>
11+
#import "Photographer+MKAnnotation.h"
1112

1213
@implementation PhotographerMapViewController
1314

@@ -36,4 +37,29 @@ -(void)reload
3637
[self.mapView addAnnotations:photographers];
3738
}
3839

40+
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
41+
{
42+
[self performSegueWithIdentifier:@"setPhotographer:" sender:view];
43+
}
44+
45+
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
46+
{
47+
if([segue.identifier isEqualToString:@"setPhotographer:"])
48+
{
49+
if([sender isKindOfClass:[MKAnnotationView class]])
50+
{
51+
MKAnnotationView *mkAView = (MKAnnotationView *)sender;
52+
if([mkAView.annotation isKindOfClass:[Photographer class]])
53+
{
54+
Photographer *photographer = mkAView.annotation;
55+
NSLog(@"%@", photographer.managedObjectContext);
56+
if([segue.destinationViewController respondsToSelector:@selector(setPhotographer:)])
57+
{
58+
[segue.destinationViewController performSelector:@selector(setPhotographer:) withObject:photographer];
59+
}
60+
}
61+
}
62+
}
63+
}
64+
3965
@end

Photomania/Photomania/PhotosByPhotographerMapViewController.m

+57
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,68 @@
77
//
88

99
#import "PhotosByPhotographerMapViewController.h"
10+
#import "Photo+MKAnnotation.h"
1011

1112
@interface PhotosByPhotographerMapViewController ()
1213

1314
@end
1415

1516
@implementation PhotosByPhotographerMapViewController
1617

18+
-(void)setPhotographer:(Photographer *)photographer
19+
{
20+
_photographer = photographer;
21+
self.title = photographer.name;
22+
if(self.view.window)
23+
{
24+
[self reload];
25+
}
26+
}
27+
28+
-(void)reload
29+
{
30+
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photo"];
31+
request.predicate = [NSPredicate predicateWithFormat:@"whoTook = %@", self.photographer];
32+
NSArray *photos = [self.photographer.managedObjectContext executeFetchRequest:request error:NULL];
33+
NSLog(@"%d", [photos count]);
34+
[self.mapView removeAnnotations:self.mapView.annotations];
35+
[self.mapView addAnnotations:photos];
36+
Photo *photo = [photos lastObject];
37+
if(photo)
38+
{
39+
self.mapView.centerCoordinate = photo.coordinate;
40+
}
41+
}
42+
43+
-(void)viewDidLoad
44+
{
45+
[super viewDidLoad];
46+
[self reload];
47+
}
48+
49+
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
50+
{
51+
[self performSegueWithIdentifier:@"setPhoto:" sender:view];
52+
}
53+
54+
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
55+
{
56+
if([segue.identifier isEqualToString:@"setPhoto:"])
57+
{
58+
if([sender isKindOfClass:[MKAnnotationView class]])
59+
{
60+
MKAnnotationView *mkAView = (MKAnnotationView *)sender;
61+
if([mkAView.annotation isKindOfClass:[Photo class]])
62+
{
63+
Photo *photo = mkAView.annotation;
64+
NSLog(@"%@", photo.managedObjectContext);
65+
if([segue.destinationViewController respondsToSelector:@selector(setPhoto:)])
66+
{
67+
[segue.destinationViewController performSelector:@selector(setPhoto:) withObject:photo];
68+
}
69+
}
70+
}
71+
}
72+
}
73+
1774
@end

0 commit comments

Comments
 (0)