Pergunta

I'm trying to have my "Main View" allow the user to pick a photo and have it show up on my "Second View". So far when the button is pushed it allows the user to pick the photo and it sets it to an imageView but when I try to make it go to a different view I get problems. Here'e my code for the user picking buttons and the bottom line is what I was trying to use to switch the views.

- (IBAction)usePhotos:(id)sender {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
    imagePicker.allowsEditing = YES;
    [self presentViewController:imagePicker animated:YES completion:nil];
    _newMedia = NO;
    [self performSegueWithIdentifier:@"switch" sender:self];
}

I've tried different ways as well and they didn't work either so I'm very stuck. Thank you in advance!

Foi útil?

Solução

As far as I understand you you need to first pick up the image. And once you have one you want to show it on the second view. If that is right your code won't work because you present image picker and you want in the same time perform segue. Try move the line:

[self performSegueWithIdentifier:@"switch" sender:self];

to

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

    UIImage *image = info[UIImagePickerControllerEditedImage];

    // If you want to dismiss this view animated you should perform segue with delay (0.5 should be enough)
    [picker dismissViewControllerAnimated:NO completion:NULL];
    // You pass an image as a parameter so you can access it in prepareForSegue: method and you can pass it to destination view
    [self performSegueWithIdentifier:@"switch" sender:image];

}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top