views:

351

answers:

2

I am attempting to allow a user to select an image from the photo gallery and assign a comment to it.

The photo class presents a UIIMagePickerController and I am able to successfully retain the image chosen by the user. However, when I create a view controller for the comment box and present/dismiss it, I am not sure how to save the value of the text the user entered because that value is declared in a separate objective-c class whose variables are not visible to my photo class.

The subroutine below is from the photo class. It is called after the user has chosen an image from the iphone photo gallery:

- (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]; 
}
A: 

Typically in this scenario there would be an NSString stored in your UserPhotoComment class that is set when the user submits the comment. Before the UserPhotoComment is dismissed you would be able to pull the NSString out of it and assign it somewhere you could later reference it again (e.g., inside a member variable of self in this routine.)

fbrereto
Thank you. In the code above, how would I pull/retain a value from the 'comment' view. It's not clear to me how to program that.
Miriam Roberts
You should not, the code above is not blocked, so the method will exit before the user has entered any value.This is why `UIImagePickerController` sends back it's result in a delegate method, and this is why you should send back result in delegate methods from your view controllers as well.
PeyloW
+2  A: 

This is a typical example of where you should implement your own delegate. Example:

@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

Now just rewrite your previous code as this:

- (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];
}

And in UserPhotoCommen you send the result back to the delegate when proper, perhaps when some text editing ends like this:

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

As seen in just these short examples both UIImagePickerController and UITextField all uses delegates to return result. This is the Cocoa way, and how Apple would do it, so you should too.

I would also rename UserPhotoComment into something like UerPhotoCommentController, because the name UserPhotoComment implies it is a model object containing a comment, not a UIViewController subclass used to retrieve the comment.

PeyloW