Dan Ramos
Dan Ramos

Reputation: 1102

performSegueWithIdentifier fires but doesn't load next view

In my storyboard I have a NavViewController which sets the root view to ViewController1. ViewController1 then has a push segue to ViewController2 with the Identifier "socialSeg". ViewController1 has a UIImageView which I use to load the camcorder. After the user records the video and selects 'Use' i want to load the next view controller. In ViewController1.m I have the following:

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    NSLog(@"Performing Segue with ID");
    [self performSegueWithIdentifier:@"socialSeg" sender:self];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"Prepping");
}

Both NSLogs run, but nothing happens on my screen. I don't see the ViewController2, and no error messages pop up. Am I doing this correct?

Related question: Should I be running performSegueWithIdentifier on self (the current ViewController), or should they be ran on self.navigationController?

Edit: I tried to shorten my question somewhat to keep the post shorter, I realized you might need more context.

Here is my full storyboard. Basically "Video View Controller" would be ViewController1 and "Social View Controller" would be ViewController2. When I click on the "Root View Controller" button (Record Video), that segue works fine, which is why I left it out. Is there any chance the "Root View Controller" could be throwing things off?

Upvotes: 3

Views: 1486

Answers (1)

Yas Tabasam
Yas Tabasam

Reputation: 10625

There is nothing wrong with your performSegue code. It seems like your picker animation is still being run when you perform this Segue. Try calling performSegue in the completion block of presentViewController by modifying your present call as given below.

[imagePickerController presentViewController:YES completion:^() {
    NSLog(@"Performing Segue with ID");
    [self performSegueWithIdentifier:@"socialSeg" sender:self];
}];

Please make sure to replace imagePickerController with the name of your popup image picker controller.

Its not ideal solution but you can also delay execute your your performSegue to make sure your image picker animation has stopped as:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   //1.5 is number of seconds its going to wait before executing the block of code
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.5 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
        NSLog(@"Performing Segue with ID");
        [self performSegueWithIdentifier:@"socialSeg" sender:self];
    });
}

NOTE: The issue is that image picker popup animation is still in progress (to close the picker after user has picked an image or video) when performSegue tries to start another animation to load next view controller. Since one animation has not finished yet therefore performSegue animation gets ignored and view controller does not load.

Here is a related question and answer: performSegueWithIdentifier not working

Upvotes: 3

Related Questions