Jack Humphries
Jack Humphries

Reputation: 13267

Difficulty accessing objects in another view controller

I'm setting a string in a view controller called ViewController and trying to access it somewhere else. This is the code:

ViewController.h

NSString *string;

...

@property (retain) NSString *string;

ViewController.m

@synthesize string;

...

-(void)viewDidLoad {

...

string = @"Test";

}

OtherViewController.m

#import "ViewController.h"

...

-(void)viewDidLoad {

    ViewController *vc;
    vc = [[ViewController alloc] init];

    NSLog(@"String: %@", vc.string);

}

However, the log is showing: String: (null). What am I doing incorrectly? Thanks.

Upvotes: 0

Views: 661

Answers (3)

Yonathan Jm
Yonathan Jm

Reputation: 442

edit : it doesn't necessarily need to be an NSObject class, if you want to, you could also do this on your viewController class, just be sure to also include

-(id)init

on your header

---- end of edit

if you're trying to make a class that's accessible to another view controller, why not try NSObject instead of view controller (considering you only need to take that string value)

for instance, lets call that viewController class "global" class

so at global.h, you put up

#import <Foundation/Foundation.h>

@interface GlobalVar : NSObject

@property (nonatomic, strong) NSString *myString;


-(id)init;

@end

and then, at global.m you put up

#import "GlobalVar.h"

@implementation GlobalVar

@synthesize myString;

-(id)init
{
    self = [super init];
    if(self)
    {
        myString = [[NSString alloc]initWithFormat:@"the String"];
    }

    return self;
}



@end

after this, everytime you need to access the "myString" object that contained in global class, you could put up

at header :

#import "GlobalVar.h"
...
...
@property (nonatomic, strong) GlobalVar *globalVar;

at implementation file :

@synthesize globalVar;

...
...

self.globalVar = [[GlobalVar alloc]init];
NSString *theString = globalVar.myString;
NSLog(@"content of my string is : %@",theString);

there you go ;)

Upvotes: 0

Paul.s
Paul.s

Reputation: 38728

The viewDidLoad of ViewController is only called when the view is loaded. The view is lazily loaded when required e.g. when a call to vc.view is made.

I'm not sure what you are trying to achieve but this certainly seems like a code smell to me.

As @Fscheidl points out you are creating a new instance and not accessing an existing instance so this may add to your problem. I still believe your main issue is that you assume viewDidLoad is being called just by creating the viewController, which is not the case

Upvotes: 1

fscheidl
fscheidl

Reputation: 2301

You do create a new instance of ViewController by calling [[ViewController alloc] init]; This instance hasn't had string even set. You have to access that exact instance of ViewController.

If you create the instance of OtherViewController directly from ViewController, you can add the following to OtherViewController.h:

#import "ViewController.h"
@property (nonatomic, retain) ViewController *previousViewController

When creating the OtherViewController, you can then set:

//alloc and init instance of OtherViewController
myOtherViewController.previousViewController = self;

In your viewDidLoad: method, you can then access your string as follows:

NSLog(@"String: %@", previousViewController.string);

Upvotes: 0

Related Questions