Alon_T
Alon_T

Reputation: 1440

How do I change view controller when returning from image picker?

I'm trying to do something simple:

  1. From controller MAViewControllerMenu I can select an existing picture
  2. After selection, I return to the MAViewControllerMenu by dismissing picker view
  3. Right upon returning to MAViewControllerMenu, I want to switch to another controller, MAViewControllerPictureDisplay, where I can see the image selected in an imageView.

However, it doesn't take me to the next view.

Here's the code in MAViewControllerMenu

MAViewControllerPictureDisplay* imageControllerView;
- (void)viewDidLoad
{

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //load next page
    imageControllerView = [[self storyboard]     instantiateViewControllerWithIdentifier:@"chosenImageController"];
}



-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    image = [info objectForKey:UIImagePickerControllerOriginalImage];//Take image from picker
    imageControllerView.image = image;//set it for the next controller's property
    [self dismissViewControllerAnimated:YES completion:^{
    [self.presentingViewController presentModalViewController:imageControllerView
                                                     animated:YES];
}];
    //NEXT LINE DOESNT WORK
    [self presentViewController:imageControllerView animated:YES completion:nil];//THAT DOESNT WORK
}

However, I set a button in my first controller, that upon a click it runs that line above through a function as following, and that one does take me to the next controller:

- (IBAction)movetonext{
    [self presentViewController:imageControllerView animated:YES completion:nil];
}

Anything I'm missing? Why does it work when invoked upon clicking a button but doesn't work when invoked without the touch event, after selecting a picture?

Thanks

Upvotes: 0

Views: 880

Answers (2)

meda
meda

Reputation: 45490

Make use of the completion handler:

-(void)imagePickerController:(UIImagePickerController *)picker 
                                  didFinishPickingMediaWithInfo:(NSDictionary *)info { 
 [self dismissViewControllerAnimated:YES completion:^{
    image = [info valueForKey:UIImagePickerControllerOriginalImage];
    imageControllerView.image = image;
    [self presentViewController:imageControllerView animated:YES completion:nil];
 }];
}

DEMO

present view

Upvotes: 3

MCMatan
MCMatan

Reputation: 8833

Try this:

Instead of Poping UIVIewController, And then presenting. Push two view controllers, and pop one.

When you call the picker:

  [self presentViewController:imageControllerView animated:NO completion:nil];
  [self presentViewController:PickerController animated:YES completion:nil];

And When you dismiss:

   [Picker dismissViewControllerAnimated:YES completion:NULL];

Note that when i call two UIViewController to present, won of them (the one not to be yet presented)is animation set to "NO".

Hope this helps

Edit

The above code, only works with UINaviationController, by "PushViewController" and "PopViewController".

The only two ways of accomplishing this, that i found are:

1 - If you user NSNotificationCenter for dismissing your middel view, as shown here:

Dismiss 2 modal view controllers

Unfortunately for you, its not the same case, so the way i would think of using UINaviationController for you, is to use a Subview for UIImagePicker, that dismisses his self..

2 - Know this is kind of a hack, but it works! if you call to present the middel UIViewController, with no animation, and from that view call the image picker, it will work. and you will have to delegate back the image picked. I added to my code, for the middel UIViewController to have Alpha set to 0 for the first time, so you will not notice, that he is the won how is presenting:

//First UIViewController:

@implementation ViewControllerOne
- (IBAction)buttonPressed:(id)sender {

    ViewControllerTwo *viewTwo = [[ViewControllerTwo alloc]init];
    [self presentViewController:viewTwo animated:NO completion:nil];
}

//Secound UIViewController:
      @interface ViewControllerTwo ()
       @property (nonatomic) UIImagePickerController *myPicker;
       @end
       @implementation ViewControllerTwo

       -(void)viewWillAppear:(BOOL)animated
       {
       //View to alph 0 if this is the first time it displayis:
       if (!self.myPicker) {
         [self.view setAlpha:0];
      }else{
        [self.view setAlpha:1];
       }
      }
      -(void)viewDidAppear:(BOOL)animated{

      if (!self.myPicker) {
        self.myPicker = [[UIImagePickerController alloc]init];
        self.myPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        self.myPicker.delegate = self;
        [self presentViewController:self.myPicker animated:YES completion:nil];
      }
      }  
       - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:    (NSDictionary *)info
      {
    [self.myPicker dismissViewControllerAnimated:YES completion:nil];
      }
      - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
      {
    [self.myPicker dismissViewControllerAnimated:YES completion:NO];
      }
      - (IBAction)ButtonTwoClicked:(id)sender {
       [self dismissViewControllerAnimated:YES completion:NO];
     }

Upvotes: 0

Related Questions