2011年3月4日 星期五

Paparazzi-Part 3

這作業原本預計一個禮拜完成,結果多花了一個禮拜。我想主要原因是 Core Data 的部份我不熟,在 Part 2 時草草帶過,使得 Part 3 時不得不好好研究一下。


 
Part 2 時對於 relationship 的用法不是很瞭解,這次重新設計了 Core Data 就好很多。


這裡值得注意的是經度緯度我在 Data Model 用的Type是 double 但產生出來的 Code 卻是 NSNumber 






@class Person;
@interface Photo : NSManagedObject
{
}
@property (nonatomic, retain) NSNumber * longitude;
@property (nonatomic, retain) NSData * data;
@property (nonatomic, retain) NSNumber * watched;
@property (nonatomic, retain) NSNumber * latitude;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) id url;
@property (nonatomic, retain) Person * author;
@end


而要取值時: theCoordinate.longitude = [self.longitude doubleValue];
注意: MKAnnotation 並不是程式自己產生的,而是為了要讓 photo 直接有 location 的功能而加入的 delegate




另外,參考 More iPhone SDK 增加了 NSArray 的 Categories Set
@interface NSArray(Set)
+ (id)arrayByOrderingSet:(NSSet *)set byKey:(NSString *)key ascending:(BOOL)ascending;
@end 

值這樣就可以順利取出 photo set 了  (NSSet 轉 NSArray)

NSArray* array = [NSArray arrayByOrderingSet:thePerson.photos byKey:@"name" ascending:YES];


Flickr 從網路抓取圖片用作業提供的 JSON 和 FlickFetcher 來達成,此次作業用同步的方式來下載,所以會有點停頓。



之前我遇到的  Hierarchy NavigationBar 問題 其實很蠢 ==
UIViewController 本身就有 navigationController 所以.... 不用這麼麻煩,下面的 code 很簡單就達成:

[self.navigationController pushViewController:photoDetailViewController animated:YES];






#pragma mark -
#pragma mark MKMapViewDelegate
// note: you can assign a specific call out accessory view, or as MKMapViewDelegate you can implement:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
//NSLog(@"%@",view.annotation);
Photo* thePhoto = (Photo*) view.annotation;
PhotoDetailViewController* photoDetailViewController = [[PhotoDetailViewController alloc] initWithNibName:@"PhotoDetailViewController" bundle:nil];
//photoDetailViewController.url = thePhoto.url;
photoDetailViewController.title = thePhoto.name;
photoDetailViewController.photo = thePhoto;
[self.navigationController setNavigationBarHidden:NO animated:NO];
[self.navigationController pushViewController:photoDetailViewController animated:YES];
[photoDetailViewController release];
}
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)
    [self.map dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView)
{
// if an existing pin view was not available, create one
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
  initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
// add a detail disclosure button to the callout which will open a new view controller page
//
// note: you can assign a specific call out accessory view, or as MKMapViewDelegate you can implement:
//  - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
//
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//[rightButton addTarget:self
// action:@selector(showDetails:)
//   forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}



Map 顯示也是比較不熟,主要是參考 Apple 範例程式修改 MapCallouts
有些照片沒有地理位置資訊,我就不知道他是怎麼判斷的了。
看起來很多圖片都是在同一個位置上。


主要是實作上面的兩個 delegates








































要有 Scroll 功能記得要設 delegate

















剩下最後一個部份了  gogogogo!!

沒有留言:

張貼留言