0

I want to print out vc1.string1 from vc2.

Currently console's showing:

vc1.string1 (null)

When I was not using storyboard I accessed vc1 variable like this:

AppDelegate *appDelegate = [(AppDelegate *)[UIApplication sharedApplication]delegate];
NSLog(@"vc1.string1 %@", appDelegate.viewController.string1);

But I don't know how to access vc1.string when I'm using storyboard.

Help please thanks.

P.S. Here's the link of my project: http://dl.dropbox.com/u/12439052/AccessDiffClass.zip

//ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    NSString *string1;
}

@property (nonatomic, strong) NSString *string1;

@end

#import "ViewController.h"

@implementation ViewController
@synthesize string1;

-(void)viewDidLoad {
    string1 = @"String One";
    NSLog(@"string1 %@", string1);
}

@end

VC2:

//ViewController2.h

#import <UIKit/UIKit.h>

@class ViewController;

@interface ViewController2 : UIViewController {
    ViewController *vc1;
}

@property (nonatomic, strong) ViewController *vc1;

@end

#import "ViewController2.h"
#import "ViewController.h"

@implementation ViewController2
@synthesize vc1;

-(void)viewDidLoad {
    NSLog(@"vc1.string1 %@", vc1.string1);
}

@end
Steve Ham
  • 3,067
  • 1
  • 29
  • 36
  • why do you wanna use strong? usually we use retain – shader Jan 03 '12 at 06:05
  • Yes I am using ARC. Is it harder or bad to work in that environment cause it's new? Or should I keep using it and going with the new feature. – Steve Ham Jan 03 '12 at 06:58
  • Thanks, @Michael Dautermann... well I am still used to managing memory by myself... – shader Jan 03 '12 at 08:00
  • I thought managing memory manually would be more memory-efficient? Wrong? – shader Jan 03 '12 at 08:02
  • @shader Yes, it is completely deterministic [About ARC](http://www.learn-cocos2d.com/2011/11/everything-know-about-arc/#myth-ARC-not-reliable) – fabian789 Jan 03 '12 at 08:14

2 Answers2

1

I downloaded your project and added this bit of code to your ViewController.m file:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog( @"preparing for segue" );
    ViewController2 * vc2 = [segue destinationViewController];
    vc2.vc1 = self;
}

And this got things appearing as you were hoping for in my simulator console.

Now, this is definitely not the best thing to do here. In the ARC world, I don't know if vc1 is retained or if we're leaking or whatever. It would be much smarter for you to give your ViewController2 class a NSString * property that gets set in the prepareForSegue method. And also give an identifier to your segue.

Here is another StackOverflow question that talks about prepareForSegue a bit more (and somewhat more detailed).

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Ah actually this is not changing the value of string1 permanently. I want to access the string1 from vc2. Before I did that by accessing Appdelegate first then going to vc1. – Steve Ham Jan 03 '12 at 08:06
  • Can you try with this project too please? http://dl.dropbox.com/u/12439052/AccessWithoutAuto.zip – Steve Ham Jan 03 '12 at 08:53
  • I was able to get your newer project to work merely by adding a `vc3.vc1 = self;` line in your `toVC3:` action method right after creating `vc3`. For something like this, especially if you are using storyboards, I'd recommend just adding a `NSString` property to your application delegate and then set & read from that instead of trying to keep your string in one particular view controller (which disappears and can be released as soon as it finishes animating off the screen). – Michael Dautermann Jan 03 '12 at 13:07
  • Thanks for keep replying http://dl.dropbox.com/u/12439052/NoStory.zip Here's one project where I make it work using ARC but without using storyboard. It's because there's some pre-written code in AppDelegate if I'm not using Storyboard. I tried @property (strong, nonatomic) ViewController *viewController; & [[ViewController alloc]init] on using ARC+Storyboard project but the string1 would come out to be as null. – Steve Ham Jan 03 '12 at 14:50
  • Hello SeungUn! I've already looked at two projects and hope that I have helped you enough so that you can determine what the best course of action for your app will be. – Michael Dautermann Jan 03 '12 at 17:55
0

made a mistake last time try this.

Key Code: appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; vc1 = appDelegate.viewController; NSLog(@"string1 %@", vc1.string1);

http://dl.dropbox.com/u/12439052/passingValue.zip

Steve Ham
  • 3,067
  • 1
  • 29
  • 36