Dismissing UIImagePickerController from UITabBarController
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 suggesti开发者_JAVA百科ons 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];
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];
}
精彩评论