0

I want to call programmatically a segue from the imagePickerController:didFinishPickingMediaWithInfo: method, but it doesn't work. It doesn't crash, but doesn't work. It just does nothing. The prepareForSegue method is called correctly.

- (void) imagePickerController:(UIImagePickerController *)picker 
         didFinishPickingMediaWithInfo:(NSDictionary *)info {

   rutaFoto = [info valueForKey:UIImagePickerControllerMediaURL];
   [self dismissModalViewControllerAnimated:YES];
   [self performSegueWithIdentifier:@"aGaleria" sender:self];
}

The segue links 2 UIViewControllers. And if I call the segue with performSegueWithIdentifier from other place (e.g. from a push UIButton method), it works. Any ideas?

2
  • What kind of segue is "aGaleria" ? Modal ? Push ? Try calling the segue in viewDidDisappear.
    – rdurand
    Nov 23, 2012 at 13:29
  • It's modal. I tried to call it from viewDidDisappear but it didn't work...
    – Xithias
    Nov 23, 2012 at 16:25

2 Answers 2

3

Try using dismissViewControllerAnimated:completion: and put the call to performSegueWithIdentifier:sender: in the completion block (dismissModalViewControllerAnimated: is depreciated anyway, so you shouldn't be using it).

      [self dismissViewControllerAnimated:YES completion:^{
           [self performSegueWithIdentifier:@"aGaleria" sender:self]; 
      }];
4
  • If I write this: [self dismissViewControllerAnimated:YES completion: [self performSegueWithIdentifier:@"aGaleria" sender:self]]; I get this error: Sending 'void' to parameter of incompatible type 'void(^)(void)'
    – Xithias
    Nov 23, 2012 at 17:34
  • Oops, sorry for my syntax error. It worked! Thank you so much! My main viewController is shown for a moment, and then goes to the Galeria viewController. Is there any way to avoid the mainViewController to be shown?
    – Xithias
    Nov 23, 2012 at 17:51
  • Have you tried doing things in the reverse order? Do the segue first, and then dismiss the imagePickerController?
    – rdelmar
    Nov 23, 2012 at 17:57
  • Yes, I tried, but it didn't work. If I call the segue before the didFinishPickingMediaWithInfo method is finished, the segue does nothing.
    – Xithias
    Nov 23, 2012 at 18:04
-2

I'm wondering if this is to do with the main thread already handling a change in the current ViewController, the transition between didFinishPickingMediaWithInfo to the original ViewController from which it was triggered from.

I'd try two things, the first is to put a delay when you call the aGaleria segue:

sleep(2.0);
[self performSegueWithIdentifier:@"aGaleria" sender:self];

The second, and much better option, is to perform the segue from the view AFTER you have finishedPickingMediaWithInfo. You could set up a new NSNotificationCenter trigger in the original ViewController and then trigger it with a delay in the didFinishPicking method:

1
  • I tried both, they didn't work. The didFinishPickingMediaWithInfo method collects the info after choosing a picture in the gallery, but the gallery controller is dismissed only when the didFinishPickingMediaWithInfo method finishes, so when I call the segue, the simulator still shows the gallery, not my main viewController. So that must be the problem, but I don't know how to solve it...
    – Xithias
    Nov 23, 2012 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.