performSegueWithIdentifier not working

2019-02-11 01:45发布

问题:

I have this code that loads a new UIView (from storyboard) when a button is pressed. goPressed function is fired on button pressed and it calls selectImage function. selectImage opens a UIImagePickerController and lets user select a photo. After user has selected the photo, didFinishPickingMediaWithInfo delegate adds the selected image to a UIImageView.

In 'goPressed', after selectImage is performed, it should be performing a Segue at like commented as //1. But nothing happens. performSegueWithIdentifier doesn't seem to be working. And if I don't call [self selectImage] before calling performSegueWithIdentifier, it works. Here is the code:

- (IBAction)goPressed:(id)sender {
    [self selectImage];
    [self performSegueWithIdentifier:@"lastView" sender:currentSender]; //1
}
-(void)selectImage
{
    // Create image picker controller
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    // Set source to the camera
    imagePicker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;

    // Delegate is self
    imagePicker.delegate = (id)self;

    // Allow editing of image ?
    imagePicker.allowsEditing=NO;


    // Show image picker
    [self presentModalViewController:imagePicker animated:YES];


}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Access the uncropped image from info dictionary
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    [[picker presentingViewController] dismissModalViewControllerAnimated:YES];


    lePreview.image= image;


}

Please help, why performSegueWithIdentifier isn't working? I hope I am not missing any information you need.

回答1:

I'm assuming that the view you are trying to segue to is using the picture you get just before you do your segue? If they cancel from the image picker do you still want to segue?

If it needs the picture, then maybe you should call your segue after the delegate call "did finish picking".

The issue with the segue not firing may be due to the animation still occurring from here:

[[picker presentingViewController] dismissModalViewControllerAnimated:YES];

You can try:

[[picker presentingViewController] dismissModalViewControllerAnimated:NO];

or if you want to maintain the animation, move the segue to the "picker did finish" method and do it this way:

[self dismissModalViewControllerAnimated:YES completion:^() {
[self performSegueWithIdentifier:@"lastView" sender:self];
}];

or if that does not work try this approach in the pickerdidfinish method (note - this should be implemented as a delegate in the controller that calls the modal view, not the modal view itself:

//maintain the animation
[self dismissModalViewControllerAnimated:YES];

//slight pause to let the modal page dismiss and then start the segue
double delayInSeconds = 0.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

    //code to be executed on the main queue after delay

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

});

I use this transition frequently and it makes a nice drop away with the modal view then slides in the segue view and the pause allows the transition to seem natural.