26
votes

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];
        }
    }
}
2
Are you certain? How are you checking? In my experience, viewWillAppear always fires for the presenting view controller when the presented modal is dismissed.geraldWilliam
I have editing my post to see what I did , What I am missing ?Ouassim Mouyarden
@OuassimMouyarden You need to learn how to think about OOP properly before you go right into things. That being said, I'll still help you, for now. What do you want to happen exactly when a button on your UIAlertView is pressed?user2643580
I want to dissmiss my modalview , and call viewwillappear of my firstviewcontroller to do some manipulationOuassim Mouyarden
The code worked for me, and viewDidAppear was called. Did the modal view controller get dismissed?rdelmar

2 Answers

13
votes

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.

12
votes

Since you're presenting the modal view controller as a form sheet, the presenting controller's view never disappears, so viewWillAppear: is not called after the dismissal. If you want the presenting view controller to handle something after the dismissal, call a delegate method in the modal controller's viewDidDisappear: method. You've already set the delegate, so I presume you already have a delegate protocol in CreateActivity.

By the way, you should use the non-deprecated methods to present and dismiss your modal view controller.