user3211165
user3211165

Reputation: 235

Handling and running multiple ViewControllers in the same method

I have a MainViewController which has a button init. On the button click a pictures starts animating, then I create a new instance of a different ViewController that runs a web request and extracts some info and push it in front. I wish the image to be animating while the middle view controller is running. Finally once the MiddleViewController is done, in the same button method I switch to a 3rd ViewController. As it is expected I get the following error:

    Warning: Attempt to present <ThirdViewController: 0x962b470> on <MainViewController: 0x986c200> while a presentation is in progress!
Unbalanced calls to begin/end appearance transitions

How could I fix this? I prefer not to remove the middle ViewController and integrate its code in the MainViewController.

-(void)singleTapping:(UIGestureRecognizer *)recognizer
{

    UIImage *rot1 = [UIImage imageNamed:@"pic1.png"];
    UIImage *rot2 = [UIImage imageNamed:@"pic2.png"];
    UIImage *rot3 = [UIImage imageNamed:@"pic3.png"];
    UIImage *rot4 = [UIImage imageNamed:@"pic4.png"];

    self.scanclickedimage.animationImages = [[NSArray alloc] initWithObjects:rot1, rot2, rot3, rot4, nil];
    self.scanclickedimage.animationDuration = 0.6f;
    self.scanclickedimage.animationRepeatCount = HUGE_VALF;

    [self.scanclickedimage startAnimating];


    if (nextString == NULL || [nextString isEqualToString:@""]) {

        NSLog(@"Initializing MiddleViewController");
        MiddleViewController * middleViewController = [[MiddleViewController alloc] init];
        middleViewController.delegate = self;

        [self presentViewController:middleViewController animated:YES completion:nil];



    } else {
        ProductWebRequest *request = [[ProductWebRequest alloc] init];
        [request sendWebRequest:nextString];
    }






    NSString * storyboardName = @"MainStoryboard";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"ThirdViewController"];
    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:vc animated:YES completion:nil];




}

Upvotes: 0

Views: 47

Answers (1)

Lapinou
Lapinou

Reputation: 1477

Sometimes, this error appears when you present / push more than 2 viewcontroller at the same time (or very fast), and the "animated" option is set to "YES".

Try to set animated to "NO" like this:

[self presentViewController:middleViewController animated:NO completion:nil];

Or do something like this:

[self presentViewController:middleViewController animated:YES completion:{
   [self presentViewController:vc animated:YES completion:nil];      
}];

You have to update your code to do this. Try to put all the stuff in a separated method like this:

-(void)singleTapping:(UIGestureRecognizer *)recognizer
{

    UIImage *rot1 = [UIImage imageNamed:@"pic1.png"];
    UIImage *rot2 = [UIImage imageNamed:@"pic2.png"];
    UIImage *rot3 = [UIImage imageNamed:@"pic3.png"];
    UIImage *rot4 = [UIImage imageNamed:@"pic4.png"];

    self.scanclickedimage.animationImages = [[NSArray alloc] initWithObjects:rot1, rot2, rot3, rot4, nil];
    self.scanclickedimage.animationDuration = 0.6f;
    self.scanclickedimage.animationRepeatCount = HUGE_VALF;

    [self.scanclickedimage startAnimating];


    if (nextString == NULL || [nextString isEqualToString:@""]) {

        NSLog(@"Initializing MiddleViewController");
        MiddleViewController * middleViewController = [[MiddleViewController alloc] init];
        middleViewController.delegate = self;

        [self presentViewController:middleViewController animated:YES completion:^{
            [self launchTest];
        }];

    } else {
        ProductWebRequest *request = [[ProductWebRequest alloc] init];
        [request sendWebRequest:nextString];
        [self launchTest];

    }
}

- (void)launchTest {

    NSString * storyboardName = @"MainStoryboard";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"ThirdViewController"];
    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:vc animated:YES completion:nil];
}

Upvotes: 1

Related Questions