Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling viewwillappear after dismissing modalviewcontroller

How can I call viewwillappear after dismissing modalviewcontroller?

Any idea please because after dismissing my viewwillappear didn't get called :

presenting my viewcontroller modally : //firsviewcontroller :

-(IBAction)AddActivity:(id)sender{


    CreateActivity *addViewController = [[CreateActivity alloc] initWithNibName:@"CreateActivity" bundle:nil];

    addViewController.delegate = self;
    addViewController.modalPresentationStyle = UIModalPresentationFormSheet;

    addViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    [self presentModalViewController:addViewController animated:YES];


    addViewController.view.superview.frame = CGRectMake(50, 260, 680, 624);

}

//secondvioewcontroller : I create An alertview to dismiss this modalview , but the viewwillapear didn't get called:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0){


        if ([self respondsToSelector:@selector(presentingViewController)]){
            [self.presentingViewController dismissModalViewControllerAnimated:YES];
        }
        else {
            [self.parentViewController dismissModalViewControllerAnimated:YES];
        }
    }
}
like image 362
Ouassim Mouyarden Avatar asked Sep 09 '25 20:09

Ouassim Mouyarden


1 Answers

presentModalViewController:animated: / dismissModalViewControllerAnimated: are deprecated. Use presentViewController:animated:completion: / dismissViewControllerAnimated:completion: instead.

You can use the completion block to execute any code post dismisal:

- (void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) buttonIndex
{
    if (buttonIndex == 0)
    {
        MyCustomViewController* mcvc = (MyCustomViewController*)self.presentingViewController;

        [self dismissViewControllerAnimated: YES completion: ^{

             // call your completion method:
             [mcvc someCustomDoneMethod];
        }];
    }
}

Better yet, if you're using a storyboard then you can implement an unwind segue and trigger your completion code in the unwind callback method.

like image 65
TomSwift Avatar answered Sep 12 '25 12:09

TomSwift