Question

I'm using Xcode 4.3.2 & iOS 5.1 simulator, and in my project I am using two View Controllers: testingViewController, and detailView. I want to set values for an NSString in detailView once I didSelect tableViewCell in testingViewController.

For that i used following code:

testingViewController.h

testingViewController.h

testingViewController.m

testingViewController.m

detailView.h

detailView.h

detailView.m

detailView.m

I get null values for both wordName and wordMeaning in the console.

Anyone have a solution? Thanks in Advance!

Was it helpful?

Solution

If you have a storyboard segue between testingViewController and detailView, in your testingViewController's prepareForSegue: method you should change line

detailView *dVw = [[detailView alloc] init]; 

to

detailView *dVw = (detailView *)segue.destinationViewController;

Also, I recommend you to change in your detailView.h file the

@property (nonatomic, assign) NSString *YOUR_STRING; 

to

@property (nonatomic, copy) NSString *YOUR_STRING;

OTHER TIPS

Use

[[segue destinationViewController] setWordMeaning:meaningString];
[[segue destinationViewController] setWordName:wordTapped];

The problem is that you are creating new object of the detailView

I wrote this for sharing data between classes. I hope it is useful for you.

Set your NSStrings to retain properties, not to assign. Also, when you change their value, always use

self.property = ...;

in order for the retain to take place. Otherwise, it will be released and you will get null or bad access.

create custom init method

declare in .h file 

    -(id)initWithWordName:(NSString*)myWordName andWordMeaning:(NSString*)myWordMeaning;

    and in .m file

    -(id)initWithWordName:(NSString*)myWordName andWordMeaning:(NSString*)myWordMeaning
    {
       self = [super init];
       if(self)
       {
         self.wordName = myWordName;
         self.wordMeaning = myWordMeaning;
       }
       return self;
    }

and also NSString property should be copy instead of assign, do change in your detail.h file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top