0

私のアプリには、mapView と詳細ビューの 2 つのビューがあります。mapView には注釈があり、注釈ビューには、コールアウト ボタンが押されたときに詳細ビューをスタックにプッシュする右のコールアウト ボタンがあります。詳細ビューでは、ユーザーは詳細ビュー コントローラーにある UIImage に表示される写真を撮ることができます。私が欲しいのは、注釈ごとに異なる画像を詳細ビューコントローラーに配置することです。シングルトンが使えると読んで、シングルトンクラスを作ったのですが、使い方がわかりません。誰かが私を助けることができますか?ここにいくつかのコードがあります:

私の MKAnnotation クラス:

#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface MapPoint : NSObject <MKAnnotation>
{
    NSString *_address;
    CLLocationCoordinate2D _coordinate;
    NSNumber *_identifier;
    UIImage *_image;
}

- (id)initWithAddress:(NSString*)address
    coordinate:(CLLocationCoordinate2D)coordinate
         title:(NSString *)t
    identifier:(NSNumber *)ident;


//This is a required property from MKAnnotation
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

//This is an optional property from MKAnnotataion
@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;
@property (nonatomic) BOOL animatesDrop;
@property (nonatomic) BOOL canShowCallout;

@property (copy) NSString *address;
@property (copy) NSNumber *identifier;
@property (copy,nonatomic) UIImage *image;

@end

@implementation MapPoint

@synthesize title, subtitle, animatesDrop, canShowCallout;
@synthesize address = _address, coordinate = _coordinate, identifier = _identifier, image = _image;

-(id)initWithAddress:(NSString *)address
   coordinate:(CLLocationCoordinate2D)coordinate
        title:(NSString *)t
   identifier:(NSNumber *)ident
{
    self = [super init];

    if (self) {
        _address = [address copy];
        _coordinate = coordinate;
        _identifier = ident;

        [self setTitle:t];

        NSDate *theDate = [NSDate date];

        subtitle = [NSDateFormatter localizedStringFromDate:theDate
                                              dateStyle:NSDateFormatterMediumStyle
                                              timeStyle:NSDateFormatterMediumStyle];
    }
    return self;
}

@end

詳細ビューコントローラーは次のとおりです。

#import <UIKit/UIKit.h>
@class P2OViewController;

@interface PinViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate,UITextFieldDelegate>

{
    __weak IBOutlet UILabel *label;
    __weak IBOutlet UIButton *removeButton;
    NSString *addressForLabel;
    __weak P2OViewController *_vc;
    __weak NSNumber *_identifier;
}

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *removeButton;
@property (weak, nonatomic) P2OViewController *vc;
@property (weak, nonatomic) NSNumber *identifier;

-(void)takePicture:(id)sender;

-(void)buttonPressed:(id)sender;

@end

#import "PinViewController.h"
#import "MapPoint.h"
#import "P2OViewController.h"

@implementation PinViewController

@synthesize imageView, label, removeButton;
@synthesize vc = _vc, identifier = _identifier;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UIBarButtonItem *pic = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
                                                                        target:self
                                                                        action:@selector(takePicture:)];
    [[self navigationItem]setRightBarButtonItem:pic];

}
return self;
}

-(void)takePicture:(id)sender
    {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];

    //If our device has a camera, we ant to take a picture, otherwise, we just pick from photo library
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    } else {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    [imagePicker setDelegate:self];

    //Place the image picker on the screen
    [self presentViewController:imagePicker
                   animated:YES
                 completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //Get picked image from info dictionary
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    //Put the image onto the screen in out image view
    [imageView setImage:image];

    //Take image picker off the screen - you must call this dismiss method
    [self dismissViewControllerAnimated:YES
                         completion:nil];

}

@end
4

1 に答える 1

2

確かにMKAnnotationプロトコルを実装するクラスがあります。

このクラスでは、UIImageプロパティを追加して、すべての注釈にそのカスタム イメージを割り当てるだけです。

次に、詳細ビューを表示するときに、注釈を渡し、定義した画像プロパティにアクセスします。

編集 あなたPinViewControllerは単にこのようなプロパティを宣言します

@property(nonatomic, strong) MapPoint * annotation;

PinViewController対応する注釈を割り当てる前に、次のようなことを行います

detailedController.annotation = theAnnotation;

画像を取得するために必要なことは何でもしてから、この方法で注釈に保存します

self.annotation.image = theImage;

これで、注釈が画像を保存するようになったので、画像をメモリに保持している限り、画像を利用できます。

次回詳細ビューをプッシュするときに、その注釈が既に画像を持っているかどうかを確認するためのチェックを実行できます

if(nil != self.annotation.image) {
     // do something
} else {
     // do something else
} 
于 2012-12-11T04:28:18.120 に答える