SILminore
SILminore

Reputation: 509

NSString between classes

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!

Upvotes: 1

Views: 1579

Answers (3)

Baig
Baig

Reputation: 5005

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 
  }

Upvotes: 0

mprivat
mprivat

Reputation: 21912

You are calling the init method, not the viewDidLoad method.

If you add this to Class_A, you'll be closer:

-(id) init {
    self = [super init];
    if(self) {
        stringToPass = @"ANOTHER FANTASTIC STRING!";   
    }

    return self;
}

Upvotes: 0

colincameron
colincameron

Reputation: 2704

- (void)viewDidLoad will not be called until after you call -(NSString *)stringPassage. Try moving stringToPass = @"ANOTHER FANTASTIC STRING!"; into the -(id)init method of Class_A

Upvotes: 1

Related Questions