Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3348762
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:34:56+00:00 2026-05-18T01:34:56+00:00

I have a mainViewController. I call [self pushModalViewController:someViewController] which makes someViewController the active view.

  • 0

I have a mainViewController. I call [self pushModalViewController:someViewController] which makes someViewController the active view.

Now I want to call a function in mainViewController as someViewController disappears with [self dismissModalViewController].

viewDidAppear does not get called probably because the view was already there, just beneath the modal view. How does one go about calling a function in the mainViewController once the modalView dismisses itself?

Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:34:56+00:00Added an answer on May 18, 2026 at 1:34 am

    This answer was rewritten/expanded to explain the 3 most important approaches (@galambalazs)

    1. Blocks

    The simplest approach is using a callback block. This is good if you only have one listener (the parent view controller) interested in the dismissal. You may even pass some data with the event.

    In MainViewController.m

    SecondViewController* svc = [[SecondViewController alloc] init];
    svc.didDismiss = ^(NSString *data) {
        // this method gets called in MainVC when your SecondVC is dismissed
        NSLog(@"Dismissed SecondViewController");
    };
    [self presentViewController:svc animated:YES completion:nil];
    

    In SecondViewController.h

    @interface MainViewController : UIViewController
        @property (nonatomic, copy) void (^didDismiss)(NSString *data);
        // ... other properties
    @end
    

    In SecondViewController.m

    - (IBAction)close:(id)sender 
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    
        if (self.didDismiss) 
            self.didDismiss(@"some extra data");
    }
    

    2. Delegation

    Delegation is the recommended pattern by Apple:

    Dismissing a Presented View Controller

    If the presented view controller must return data to the presenting view controller, use the delegation design pattern to facilitate the transfer. Delegation makes it easier to reuse view controllers in different parts of your app. With delegation, the presented view controller stores a reference to a delegate object that implements methods from a formal protocol. As it gathers results, the presented view controller calls those methods on its delegate. In a typical implementation, the presenting view controller makes itself the delegate of its presented view controller.

    MainViewController

    In MainViewController.h

    @interface MainViewController : UIViewController <SecondViewControllerDelegate>
        - (void)didDismissViewController:(UIViewController*)vc;
        // ... properties
    @end
    

    Somewhere in MainViewController.m (presenting)

    SecondViewController* svc = [[SecondViewController alloc] init];
    svc.delegate = self;
    [self presentViewController:svc animated:YES completion:nil];
    

    Somewhere else in MainViewController.m (being told about the dismissal)

    - (void)didDismissViewController:(UIViewController*)vc
    {
        // this method gets called in MainVC when your SecondVC is dismissed
        NSLog(@"Dismissed SecondViewController");
    }
    

    SecondViewController

    In SecondViewController.h

    @protocol SecondViewControllerDelegate <NSObject>
    - (void)didDismissViewController:(UIViewController*)vc;
    @end
    
    @interface SecondViewController : UIViewController
    @property (nonatomic, weak) id<SecondViewControllerDelegate> delegate;
    // ... other properties
    @end
    

    Somewhere in SecondViewController.m

    [self.delegate myActionFromViewController:self];
    [self dismissViewControllerAnimated:YES completion:nil];
    

    (note: the protocol with didDismissViewController: method could be reused throughout your app)


    3. Notifications

    Another solution is sending an NSNotification. This is a valid approach as well, it might be easier than delegation in case you only want to notify about the dismissal without passing much data. But it’s main use case is when you want multiple listeners for the dismissal event (other than just the parent view controller).

    But make sure to always remove yourself from NSNotificationCentre after you are done! Otherwise you risk of crashing by being called for notifications even after you are deallocated. [editor’s note]

    In MainViewController.m

    - (IBAction)showSecondViewController:(id)sender 
    {
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        [self presentViewController:secondVC animated:YES completion:nil];
    
        // Set self to listen for the message "SecondViewControllerDismissed"
        // and run a method when this message is detected
        [[NSNotificationCenter defaultCenter] 
         addObserver:self
         selector:@selector(didDismissSecondViewController)
         name:@"SecondViewControllerDismissed"
         object:nil];
    }
    
    - (void)dealloc
    {
        // simply unsubscribe from *all* notifications upon being deallocated
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    } 
    
    - (void)didDismissSecondViewController 
    {
        // this method gets called in MainVC when your SecondVC is dismissed
        NSLog(@"Dismissed SecondViewController");
    }
    

    In SecondViewController.m

    - (IBAction)close:(id)sender 
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    
        // This sends a message through the NSNotificationCenter 
        // to any listeners for "SecondViewControllerDismissed"
        [[NSNotificationCenter defaultCenter] 
         postNotificationName:@"SecondViewControllerDismissed" 
         object:nil userInfo:nil];
    }
    

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this MainViewController, which is a controller for my table view. Whenever I
I have added a subview in my MainViewController. How can i call a method
I have a modal view controller name: ComposeMessageViewController. And main view controller name: MainViewController.
I have a viewController called MainViewController, which has a UIButton. When tapped, this button
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I'm having some trouble figuring out to call methods that I have in other
I have a UIViewController which when it loads it loads up this.. MapViewController *mapController
I'm currently working on an app with this structure: I have a view controller
I'm creating an app with one UIViewController and many UIViews. I have MainViewController with
I have a View Controller inside my MainWindow.xib file that loads the nib Cover

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.