user916367
user916367

Reputation:

String Problems

A very stupid question from a noob.

I have an action, that sets the string of a label.

- (IBAction) changeProductText:(NSString *)str{ lblProductTxt.text = str; }

This is the string I want to set that to:

TestText = [NSString stringWithFormat:@"Hi"];

And this is how I am doing it:

[self.navigationController pushViewController:nextController animated:YES];

[nextController changeProductText:TestText];

My problem is that it wont set the string to anything is random whats going into the string. It may crash when I click on the cell it may not, so I am doing something wrong.

Upvotes: 0

Views: 113

Answers (3)

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

The only parameter of an IBAction is the sender:

- (IBAction) clickMyButton: (id) sender;

A string is hardly a valid sender for an action, so whatever you are setting to lblProductTxt.text, it is not a string, it is the sender that performs the action.

In your action method, you can of course set lblProductTxt.text. You'll have to find out yourself where you get the string.

Update

From your comments I deduce you don't have an IBAction, you simply have a void method. Your use of IBAction got me on the wrong foot. Declare it like:

- (void) changeProductText: (NSString *) newText;

Omit the (IBAction) designator, as that is only necessary for, well, real IB action methods.

No matter if you use

NSString *testText = [NSString stringWithFormat: @"Hi"];

or

NSString *testText = [NSString stringWithString: @"Hi"];

The result is exactly the same: an autoreleased NSString with the text "Hi". Only the way it was created is slightly different. If one works and the other crashes, then the same thing is wrong and you are just lucky it doesn't crash.

Now what is wrong is impossible to see from what you posted so far.

Upvotes: 0

Hermann Klecker
Hermann Klecker

Reputation: 14068

Is this the exact sequence of the statements?

[self.navigationController pushViewController:nextController animated:YES];
[nextController changeProductText:TestText];

I am not 100% sure but I believe that the second line will not be executed before the nextController is being pushed. Try to reverse them. (1st create and initialize the nextController) 2nd assign all values that you want to pass down to nextController 3rd push nextViewController on the stack of View Controllers.

[nextController changeProductText:TestText];
[self.navigationController pushViewController:nextController animated:YES];

Upvotes: 0

crackity_jones
crackity_jones

Reputation: 1077

stringWithFormat gives you an autoreleased format, without seeing more of the code I'm guessing its hitting an autorelease pool and you're trying to access garbage that was your string.

Upvotes: 1

Related Questions