2009-08-28 9 views
1

ユーザーが写真ギャラリーから画像を選択してコメントを付けることを許可しようとしています。2つのModalViewControllersで変数の値を保持

写真のクラスはUIIMagePickerControllerを表示し、ユーザーが選択した画像を正常に保持できます。しかし、コメントボックスのビューコントローラを作成して表示/非表示にすると、変数が表示されない別のobjective-cクラスで宣言されているため、ユーザーが入力したテキストの値を保存する方法がわかりません私の写真のクラスに。

以下のサブルーチンは写真クラスのものです。

- (void)imagePickerController:(UIImagePickerController*)picker 
    didFinishPickingImage:(UIImage*)image 
       editingInfo:(NSDictionary*)editingInfo 
{ 
    // I do something with the image here 
    [self uploadImage]; 
    // now I dismiss the modalviewcontroller for the photo library 
    [self dismissModalViewControllerAnimated:YES]; 
    // creating and displaying the view that will allow a user to enter a comment 
    UserPhotoComment* comment = [[UserPhotoComment alloc] init]; 
    [self presentModalViewController:comment animated:YES]; 
    // how do I get the value entered by user? The text is in another obj-c class. 
    // dismissing the modal view for the comments 
    [self dismissModalViewControllerAnimated:YES]; 
    [picker release]; 
} 

答えて

2

これは典型的な例であります独自のデリゲートを実装する必要があります。例:

@protocol UserPhotoCommentDelegate; 

@interface UserPhotoComment : UIViewController { 
    id<UserPhotoCommentDelegate> delegate; 
    // ... more instance variables. 
} 
@property(nonatomic,assign) id<UserPhotoCommentDelegate> delegate; 
// More interface definitions... 
@end 

@protocol UserPhotoCommentDelegate 
-(void)userPhotoComment:(UserPhotoComment*)userPhotoComment 
      didAddComment:(NSString*)comment; 
@end 

今、ちょうどこのよう以前のコードを書き換える:

- (void)imagePickerController:(UIImagePickerController*)picker 
     didFinishPickingImage:(UIImage*)image 
        editingInfo:(NSDictionary*)editingInfo 
{ 
    // I do something with the image here 
    [self uploadImage]; 
    // now I dismiss the modalviewcontroller for the photo library 
    [self dismissModalViewControllerAnimated:YES]; 
    // creating and displaying the view that will allow a user to enter a comment 
    UserPhotoComment* comment = [[UserPhotoComment alloc] init]; 
    comment.delegate = self; 
    [self presentModalViewController:comment animated:YES]; 
    [comment release]; // Owned by self now! 
    //[picker release]; // If properly releases as comment is, this is not needed here. 
} 

-(void)userPhotoComment:(UserPhotoComment*)userPhotoComment 
      didAddComment:(NSString*)comment 
{ 
    // Do what you need with the comment in comment. 
    [self dismissModalViewControllerAnimated:YES]; 
} 

、適切には、おそらくいくつかのテキスト編集は次のように終了したときときUserPhotoCommenにあなたがデリゲートに戻って結果を送信

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    [self.delegate userPhotoComment:self didAddComment:textField.text]; 
} 

これらの短い例に見られるように、UIImagePickerControllerUITextFieldはいずれも結果を返すために代理人を使用します。これはココアの方法であり、Appleがどのようにそれを行うのかです。

UserPhotoCommentが、それはコメントではなく、コメントを取得するために使用UIViewControllerサブクラスを含むモデルオブジェクトである意味ので、私はまた、UerPhotoCommentControllerのようなものにUserPhotoCommentの名前を変更します。

-1

通常、このシナリオでは、ユーザーがコメントを送信したときに設定されているあなたのUserPhotoCommentクラスに保存されているNSStringがあるでしょう:ユーザーがiPhoneのフォトギャラリーから画像を選択した後に呼び出されます。 UserPhotoCommentが却下される前に、あなたが後で再びそれを参照することができ、どこかにそれのうちNSStringを引くと割り当てることができるであろう(このルーチンでselfのメンバ変数の内側に、例えば。)

+0

ありがとうございます。 上記のコードでは、「コメント」ビューからどのように値を引き出し/保持しますか?それをプログラムする方法は私には分かりません。 –

+0

上記のコードはブロックされていないので、ユーザーが任意の値を入力する前にメソッドが終了しないようにしてください。 これは、 'UIImagePickerController'がデリゲートメソッドの結果を返す理由です。そのため、View Controllerのデリゲートメソッドで結果を返す必要があります。 – PeyloW

関連する問題