This worked in iOS6, so not sure what the issue is.
inside my UINavigationController (ioNavController) I present the UIImagePickerController with the Following:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.modalInPopover = YES;
imagePicker.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:imagePicker animated:NO completion:^{ }];
in my UIImagePickerControllerDelegate (which does get called)I have the Following:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
//This does not work
[ioNavController dismissViewControllerAnimated:YES completion:^{ /* Cleanup if Needed */ }];
//This does not work
[[picker parentViewController] dismissViewControllerAnimated:YES completion:^{ /* Cleanup if Needed */ }];
//This does not work
[picker removeFromParentViewController];
// This presents a new view on the Nav Controller. It shows the new view ontop of the ImagePicker. Image Picker View does not repond to touches.
[ioNavController pageToSelect:0];
}
Your delegate it's in the same controller that called the imagepicker? The same controller that called presentViewController should call the line below, and the imagepicker will be removed correctly.
[self dismissViewControllerAnimated:YES completion:nil]; //self here it's the same reference that called presentViewController
let me know if worked or helped.
The UIImagePickerController will remove itself when you use this code:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)pPicker {
[pPicker dismissViewControllerAnimated:YES completition:NULL]; }
Related
This seems like a simple question and I've seen similar ones answered but the answers just don't work (at least anymore). I have the following code in viewDidAppear:
UIImagePickerController *imagePicker;
imagePicker =[[UIImagePickerController alloc] init];
imagePicker.allowsEditing = YES;
{imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;}
imagePicker.delegate=self;
NSLog(#"ViewDidAppear");
[self presentModalViewController:imagePicker animated:YES];
NSLog (#"Modal Window Displayed.");}
This allows the user to select an image from the image library, which works. Once that image is picked, didFinishPickingMediaWithInfo gets called which has this code at the top of it:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSLog(#"Image Is Picked");
[[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];
[self dismissModalViewControllerAnimated:YES];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
[self.navigationController popViewControllerAnimated:YES];
As you can see, I've tried several different ways found here on StackOverflow to get this to work. They all get ignored. No errors, just nothing happens; the picker view never goes away. What gives? I'm using xCode 7.2.1.
I have a similar place in my code where I present another modal view and also can't get rid of it.
The problem is that the didFinishPickingMediaWithInfo method doesn't actually dismiss the other view until it is DONE with it's execution (even if what's running in that method takes almost a minute!). By the time that method was done, I had already presented another viewcontroller modally. Almost all of the techniques I'd been trying all along actually still work.
If you present with presentViewController instead of presentModalViewController:
[self presentViewController:imagePicker animated: YES];
then
[self dismissViewControllerAnimated:YES completion:nil];
should work - perhaps the combination of different functions you are calling all at once is confusing things.
If this doesn't work, perhaps try presenting it with a slight delay with dispatch_after - it could be that presenting it within viewDidAppear is causing the issue:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.001 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
[self presentViewController:imagePicker animated: YES];
}
Below code works for me perfectly. Hope it helps.
in class.h
#interface class : UIViewController<UIImagePickerControllerDelegate>
{
UIImagePickerController *picker;
}
in class.m when getPhoto called, PickerViewController presented.
- (IBAction)getPhoto:(UIButton *)sender {
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker2 didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker2 dismissViewControllerAnimated:YES completion:nil];
}
Globally declare your imagePicker like below
#implementation ViewController{
UIImagePickerController *imagePicker;
}
Display your image picker declared globally
- (IBAction)chooosePic:(id)sender {
imagePicker =[[UIImagePickerController alloc] init];
imagePicker.allowsEditing = YES; {imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;}
imagePicker.delegate=self;
NSLog(#"ViewDidAppear");
[self presentModalViewController:imagePicker animated:YES];
NSLog (#"Modal Window Displayed.");
}
didFinishPickingMediaWithInfo method update as below:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[imagePicker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Will Work
I have a viewController VC1 where I present an UIImagePickerController with sourcetype camera.. then after taking a picture, transition to a new viewController where the image is used. The problem is, how can I dismiss the imagePicker AND seamlessly transition to my new viewController. For example in my
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
method, I attempt to use either of the two methods to dismiss the picker and present the new viewController seamlessly
[self.presentedViewController presentViewController:postControl animated:NO completion:^{
[picker dismissViewControllerAnimated:NO completion:nil];
}];
/*
[picker dismissViewControllerAnimated:NO completion:^{
[self presentViewController:postControl animated:NO completion:nil];
}];
*/
Obviously the first one is wrong because I am asking the presenting view controller to dismiss itself during its animation, resulting in an error. If I change picker to self.presentingViewController it never dismisses the camera and the second bit of code(the one commented) works, but not seamlessly, In other words, You can SEE the transition.
Sorry for not understanding this problem but I am just not seeing it. How can I dismiss the camera AFTER or BEFORE presenting postControl seamlessly?
As in, you cannot see the transition?
UIViewController* toPresentViewController = [[UIViewController alloc] init];
[picker dismissViewControllerAnimated:YES completion:^
{
[self.presentingViewController presentViewController:toPresentViewController animated:YES completion:nil]
}];
I have a navigation controller that starts my app (rootViewController is the navigationController). Then inside one of the views of the navigation I call:
TabBarController *tab = [[TabBarController alloc] init];
// Presentation
[self presentViewController:tab animated:NO completion:nil];
Then one of the tabs calls the UIImagePickerController and then saves the image on another thread. Then I return to the main queue and run:
dispatch_async(dispatch_get_main_queue(), ^{
[picker dismissViewControllerAnimated:YES completion:nil];
PostViewController *post = [[PostViewController alloc] init];
// Presentation
[self presentViewController:post animated:NO completion:nil];
});
But the post view never gets called and the viewDidLoad never gets hit in the PostViewController.m. Instead the imagePicker disappears and returns to the tabBarController. How do I fix this?
Assuming that you PostViewController object is not nil , Present the view controller after the dismiss process of picker ViewController is completed . Try this Code
dispatch_async(dispatch_get_main_queue(), ^{
[picker dismissViewControllerAnimated:YES completion:^{
PostViewController *post = [[PostViewController alloc] init];
// Presentation
[self presentViewController:post animated:NO completion:nil];
}];
});
I have been developing an iPhone app and have come across a few issues. I do not have a storyboard in my app and have nothing in my xibs. I have initialised and set everything up through code. When I go to the GameViewController from my main viewcontroller everything is fine, however when I come back through my back button I get this issue:
Presenting view controllers on detached view controllers is discouraged .
When I re-arrive to the main view controller, there are little changes such as the view changing before its supposed to. Here is the code for the button on my
GameViewController *game = [[GameViewController alloc] initWithNibName:nil bundle:Nil];
[self dismissViewControllerAnimated:YES completion:NULL];
[self presentViewController:game animated:YES completion:NULL];
Here is the code for the back button:
ViewController *home = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self dismissViewControllerAnimated:YES completion:NULL];
[self presentViewController:home animated:NO completion:NULL];
If anyone can help me to see what I am doing wrong that would be great.
You can not present home view controller from self because it is already dismissed. You should change
[self dismissViewControllerAnimated:YES completion:NULL];
[self presentViewController:home animated:NO completion:NULL];
to
UIViewController *parentViewController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^
{
[parentViewController presentViewController:home animated:NO completion:nil];
}];
I'm a bit confused, this isn't a question on how to dismiss a UIImagePickerController, it's more of a "why did that work" type of question. I am working in iOS7.
Looking online at the apple documentation (this link: https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/CameraAndPhotoLib_TopicsForIOS/Articles/TakingPicturesAndMovies.html#//apple_ref/doc/uid/TP40010406-SW6), I came across the following code for dismissing a UIImagePickerController:
[[picker parentViewController] dismissModalViewControllerAnimated: YES];
Edit: I know that that method was deprecated, so instead I tried the following, and that did not work either:
[[picker parentViewController] dismissViewControllerAnimated:YES completion:NULL];
Those didn't work for me. This however did work:
[picker dismissViewControllerAnimated:YES completion:NULL];
Is the apple documentation wrong? or just outdated?
Why does the second bit of code work, because from my understanding, its the parent view controllers job to dismiss something like a popover or in this case a UIImagePickerController
Thank you.
Edit: This is how I am presenting the UIImagePickerController, the UITapGestureRecognizer calls the first method which then calls the second.
- (IBAction)captureMoment:(UITapGestureRecognizer *)sender {
[self startCameraCaptureFromViewController:self usingDelegate:self];
}
- (BOOL)startCameraCaptureFromViewController:(UIViewController *)controller
usingDelegate:(id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>)delegate {
if (delegate == nil || controller == nil || [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) {
return NO;
}
UIImagePickerController *camera = [[UIImagePickerController alloc] init];
camera.sourceType = UIImagePickerControllerSourceTypeCamera;
camera.allowsEditing = NO;
camera.delegate = delegate;
[controller presentViewController:camera animated:YES completion:NULL];
return YES;
}
There are 2 methods that worked for me
Use picker.presentingViewController instead of picker.parentViewController, answer by rmaddy
Use the following code in the imagePickerControllerDidCancel:, [picker dismissViewControllerAnimated:YES completion:NULL];
Here is the code in its entirety, this is placed in the UIViewController that presents the UIImagePickerController:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
}
The method dismissModalViewControllerAnimated has been deprecated since iOS6. The new method you are using is the correct one.