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:


NSString *string; ... @property (retain) NSString *string;


@synthesize string; ... -(void)viewDidLoad { ... string = @"Test"; }


#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.

-------------Problems Reply------------

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


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;



and then, at global.m you put up

#import "GlobalVar.h"

@implementation GlobalVar

@synthesize myString;

self = [super init];
myString = [[NSString alloc]initWithFormat:@"the String"];

return self;


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 ;)

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

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);

Category:iphone Views:0 Time:2012-02-22

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.215 (s). 11 q(s)