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 8619031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:14:50+00:00 2026-06-12T06:14:50+00:00

I need to pass a NSString between two Classes (from ViewController Class_A to ViewController

  • 0

I need to pass a NSString between two Classes (from ViewController Class_A to ViewController Class_B) of my app, I have searched on StackOverflow but this is the simplified code of my situation:

Class_A.h

#import "Class_B.h"

@interface ViewController : UIViewController {
    NSString *stringToPass;
}

@property (retain, nonatomic) NSString *stringToPass;
-(NSString *)stringPassage:(id)sender;

@end

Class_A.m

- (NSString *)stringPassage:(id)sender{
    NSString *string = @"A FANTASTIC STRING!"; //here I define string INTO the - (NSString *) method
    NSLog(@"String to pass: %@",string);
    return string;
}

Class_B.h

#import "Class_A.h"

Class_B.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    Class_A *Class_A_Instance = [[Class_A alloc] init];
    NSString *importedString = [Class_A_Instance stringPassage:self];
    [Class_A_Instance release];

    NSLog(@"Received String: %@",importedString);

    // Do any additional setup after loading the view from its nib.
}

Well, it works like a charm! When Class_B is loaded I can read in the log output “String to pass: A FANTASTIC STRING!” and “Received String: A FANTASTIC STRING!”; but if I change only this parts of code (I need it!):

Class_A.m

 - (void)viewDidLoad
{
    [super viewDidLoad];

    stringToPass = @"ANOTHER FANTASTIC STRING!";

        // Do any additional setup after loading the view, typically from a nib.
   }

- (NSString *)stringPassage:(id)sender{
    NSString *string = stringToPass; // here I must call the string FROM another method of Class_A (in this example from - (void)viewDidLoad
    NSLog(@"String to pass: %@",string);
    return string;
}

I can read in the log output “String to pass: (null)” and obv “Received String: (null)”; Its incredible because I can pass the global variable *stringToPass to every method of Class_A perfectly, except for this -(NSString *) method. So what is the problem? Thanks!

  • 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-06-12T06:14:52+00:00Added an answer on June 12, 2026 at 6:14 am

    Let we have two viewControllers i.e FirstVC and the SecondVC.
    Make a string in SecondVC ,

    @interface SecondVC : UIViewController { NSString *string2pass; }
    
    @property (retain, nonatomic) NSString *string2pass;
    

    In FirstVC when you are going to present SecondVC, pass the string as,

    - (void)presentSecondView 
      { 
        SecondVC *secondVC = [[SecondVC alloc]initWithNibName:@"SecondVC" bundle:nil]; 
    
        secondVC.string2pass = @"this is a passing string";
    
       [self presentModalViewController:resView animated:YES];
    
      }    
    - (void)viewDidLoad 
      { 
          NSString *string4mFirstVC = string2Pass 
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to pass a data between two view controllers.here is my code. for
I need to pass an extra parameter :mobilejs => true from jQuery to a
I need to pass a variable which is captured from client side via jquery
I need to pass a parameter from an EditText and when I click the
I need to pass a string as an argument but I dont know how..Help?
http://something.php?ownerID=13&view=userAgreement i need to pass two variables to this .. how can i pass
need help from some gurus here, I have a database which I have moved
I have this error, when I am trying to pass a uitextfeild value from
I have a map view with pins annotations into it. I need to pass
I have some C classes imported into my app. So along with my .h

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.