views:

61

answers:

1

I have a tab bar application whereby one tab uses a navigation controller to move through a series of views. On the final view, there is a button to add a photo, which presents a UIImagePickerController. So far, so good - however when I finish picking the image, or cancel the operation, the previous view is loaded, but without the tab bar. I'm sure I'm missing something elementary, but any suggestions on how to properly release the UIImagePickerController would be much appreciated. The code is as follows:

ImagePickerViewController *aController = [[ImagePickerViewController alloc];             initWithNibName:@"ImagePickerViewController" bundle:[NSBundle mainBundle]];  
[self presentModalViewController:aController animated:YES];  
[aController release];  

//viewDidLoad
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;

if([UIImagePickerController isSourceTypeAvailable:  UIImagePickerControllerSourceTypeCamera]){
      imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
 } else {
      imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
 }

 [window addSubview:imagePickerController.view];

//ImagePickerViewController imagePickerControllerDidCancel - FinalViewController is the last view in the stack controlled by a navigation controller which contains the button to present the UIImagePickerController

[picker dismissModalViewControllerAnimated:YES];
FinalViewController *aController = [[FinalViewController alloc initWithNibName:@"FinalViewController" bundle:[NSBundle mainBundle]];
[picker presentModalViewController:aController animated:YES];
[aController release];
A: 

You do not need to add the picker view as a subview of the window. When the user presses the button, execute something similar to the following snapPicture method:

- (IBAction) snapPicture{


            UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
            // Set up the source 
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
                ipc.sourceType = UIImagePickerControllerSourceTypeCamera; 
                ipc.allowsEditing = NO;
            }
            else {
                    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
                 }

            ipc.delegate = self;
            [self presentModalViewController:ipc animated:YES]; 
            [ipc release];


}

Then, implements the picker delegate methods. here I am just presenting one of them to show how to dismiss the picker.

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagePicker 
{ 

    [[imagePicker parentViewController] dismissModalViewControllerAnimated:YES]; 

} 
unforgiven
This was a huge help, both for the immediate problem, but also for my understanding modal view controllers. Thanks so much.
Dave
Well, if the answer helped, then you may consider voting it and may be, accepting it as definitive ;-)
unforgiven