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];
}
This is a typical example of where you should implement your own delegate. Example:
Now just rewrite your previous code as this:
And in UserPhotoCommen you send the result back to the delegate when proper, perhaps when some text editing ends like this:
As seen in just these short examples both
UIImagePickerControllerandUITextFieldall uses delegates to return result. This is the Cocoa way, and how Apple would do it, so you should too.I would also rename
UserPhotoCommentinto something likeUerPhotoCommentController, because the nameUserPhotoCommentimplies it is a model object containing a comment, not aUIViewControllersubclass used to retrieve the comment.