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

  • Home
  • SEARCH
  • 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 5972479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:39:49+00:00 2026-05-22T20:39:49+00:00

I have two view controllers, firstViewController and secondViewController . I am using this code

  • 0

I have two view controllers, firstViewController and secondViewController. I am using this code to switch to my secondViewController (I am also passing a string to it):

secondViewController *second = [[secondViewController alloc] initWithNibName:nil bundle:nil];

second.myString = @"This text is passed from firstViewController!";

second.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

[self presentModalViewController:second animated:YES];

[second release];

I then use this code in secondViewController to switch back to the firstViewController:

[self dismissModalViewControllerAnimated:YES];

All of this works fine. My question is, how would I pass data to the firstViewController? I would like to pass a different string into the firstViewController from the secondViewController.

  • 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-22T20:39:49+00:00Added an answer on May 22, 2026 at 8:39 pm

    You need to use delegate protocols… Here’s how to do it:

    Declare a protocol in your secondViewController’s header file. It should look like this:

    #import <UIKit/UIKit.h>
    
    @protocol SecondDelegate <NSObject>
    -(void)secondViewControllerDismissed:(NSString *)stringForFirst
    @end
    
    
    @interface SecondViewController : UIViewController
    {
        id myDelegate;  
    }
    
    @property (nonatomic, assign) id<SecondDelegate>    myDelegate;
    

    Don’t forget to synthesize the myDelegate in your implementation (SecondViewController.m) file:

    @synthesize myDelegate;
    

    In your FirstViewController’s header file subscribe to the SecondDelegate protocol by doing this:

    #import "SecondViewController.h"
    
    @interface FirstViewController:UIViewController <SecondDelegate>
    

    Now when you instantiate SecondViewController in FirstViewController you should do the following:

    // If you're using a view controller built with Interface Builder.
    SecondViewController *second = [[SecondViewController alloc] initWithNibName:"SecondViewController" bundle:[NSBundle mainBundle]];
    // If you're using a view controller built programmatically.
    SecondViewController *second = [SecondViewController new]; // Convenience initializer that uses alloc] init]
    second.myString = @"This text is passed from firstViewController!";
    second.myDelegate = self;
    second.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:second animated:YES];
    [second release];
    

    Lastly, in the implementation file for your first view controller (FirstViewController.m) implement the SecondDelegate’s method for secondViewControllerDismissed:

    - (void)secondViewControllerDismissed:(NSString *)stringForFirst
    {
        NSString *thisIsTheDesiredString = stringForFirst; //And there you have it.....
    }
    

    Now when you’re about to dismiss the second view controller you want to invoke the method implemented in the first view controller. This part is simple. All you do is, in your second view controller, add some code before the dismiss code:

    if([self.myDelegate respondsToSelector:@selector(secondViewControllerDismissed:)])
    {
        [self.myDelegate secondViewControllerDismissed:@"THIS IS THE STRING TO SEND!!!"];
    }
    [self dismissModalViewControllerAnimated:YES];
    

    Delegate protocols are EXTREMELY, EXTREMELY, EXTREMELY useful. It would do you good to familiarize yourself with them 🙂

    NSNotifications are another way to do this, but as a best practice, I prefer using it when I want to communicate across multiple viewControllers or objects. Here’s an answer I posted earlier if you’re curious about using NSNotifications: Firing events accross multiple viewcontrollers from a thread in the appdelegate

    EDIT:

    If you want to pass multiple arguments, the code before dismiss looks like this:

    if([self.myDelegate respondsToSelector:@selector(secondViewControllerDismissed:argument2:argument3:)])
    {
        [self.myDelegate secondViewControllerDismissed:@"THIS IS THE STRING TO SEND!!!" argument2:someObject argument3:anotherObject];
    }
    [self dismissModalViewControllerAnimated:YES];
    

    This means that your SecondDelegate method implementation inside your firstViewController will now look like:

    - (void) secondViewControllerDismissed:(NSString*)stringForFirst argument2:(NSObject*)inObject1 argument3:(NSObject*)inObject2
    {
        NSString thisIsTheDesiredString = stringForFirst;
        NSObject desiredObject1 = inObject1;
        //....and so on
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two view controllers name RootViewController and SecondViewController. In the FirstViewController I have
lets say i have two view controllers, firstViewController and secondViewController. The firstViewController is supposed
i have got two view. First: FirstViewController Second: SecondViewController FirstViewController is my UINavigationController 's
I have two view controllers: firstViewController and secondViewController. On the firstViewController I have a
So i have two view controller in FirstViewController and SecondViewController in FirstViewController NSMutableArray *array;
Hi i have two view controller: FirstViewController and SecondViewController FirstViewController.h #import <UIKit/UIKit.h> @interface FirstViewController:
Hey guys I need some help with this: I have two view controllers, let's
I have two view controllers in a tabbar which can both edit data. Therefore,
I have two view controllers that allow changes to the Address Book. The first
I have a viewController (Planner) that loads two view controllers (InfoEditor and MonthlyPlan) when

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.