views:

187

answers:

4

Hello, I try to pass a NSString from one view to an other view, but I doesn't work. I set a NSString in the SecondViewController as property

@property (assign) NSString * wert1;

When I load the SecondViewController with a button-press on the FirstViewController, I try to pass the NSString:

SecondViewController *Second = [[SpinViewController alloc] initWithNibName:nil bundle:nil];
Second.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"black.png"]];
[Second setWert1:texteingabe1]; //HERE <<<<
Second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:Second animated:YES];
[Second release];

In the SecondViewController i make this:

NSLog(@"%@",wert1);

But NSLog just says: (null). Why?

Thanks for your help and sorry for my bad English.

+3  A: 

Where are you calling that NSLog statement? If it's in loadView or viewDidLoad, it won't work, because those methods are called before setWert1: is called in your example.

Also, make sure you aren't setting it to nil somewhere else before the NSLog is called, and of course, make sure that texteingabe1 is, in fact, not nil when passing it in.

Ed Marty
+1  A: 

Does it work if you set a raw string literal? I.e [Second setWert1:@"Foo"]? Without seeing more of the code, the most likely reason for the string value to be NULL, inside the second view, is that you passed in a NULL string for 'texteingabe1'.

James Turner
Doesn't work, sorry.
Flocked
+1  A: 

You need to copy or retain the string in your Second controller. Use the @property keyword "retain" instead of "assign".

Assign just assigns the string to your instance member in Second... then when the string is released in the First controller it also disappears from Second.

Niels Castle
Doesn't work with retain.
Flocked
A: 

Got the solution. I just moved the background-code under the setWert1-code and now it works :)

Flocked
Any insights into why it works would be great...
Niels Castle