I can’t belice I am asking this, but im very crazy with this variable stuff.
I want to save a username information. I load them at the load of my view. After this I will get the information when I click on a button.
Here is my code:
@interface SelfViewController : UIViewController
{
NSString *usernameString;
}
- (void)viewDidLoad
{
usernameString = @"test";
}
- (IBAction)goToProfileButtonPressed:(id)sender
{
NSLog(@"%@", usernameString); //CRASH BAD EXEC
}
What did I wrong????
Change your .h file to:
Then in your .m file:
The problem is that you assign
usernameStringto an autoreleased string object. By the time the button is pressed,usernameStringhas been released and has become garbage memory. By retaining it (and subsequently releasing it in-deallocto avoid a leak) you know that it will not be prematurely released.