everyone, i have no idea why this doesn’t work hopefully someone here can help me out.
Here’s what i’m trying to do:
I’ve got two ViewControllers, lets call them secondViewController and firstViewController.
The firstViewController is shown when the application starts and it pushes the secondViewController.
Inside the secondViewController i defined a property.
Now i want to change this property in the firstViewController and then push the secondViewController but for some reason as soon as the secondViewController is pushed my property is reseted to zero…. why is this happening? heres my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
secondViewController.test = 2;
[self.navigationController pushViewController:secondViewController animated:YES];
}
The test property in this example is just a simple NSInteger. Now as soon the secondViewController is pushed the “test” value is no longer two it’s zero…..
Why????
here’s the header file of my secondViewController:
#import
@interface secondViewController : UIViewController {
NSInteger test;
}
@property NSInteger test;
@end
here’s the .m file:
- (void) viewDidLoad{
NSLog(@"%i", self.test):
}
If you’re checking it from the
viewDidLoadtestmay not always be 2. I actually don’t understand why that is either. Example:This will sometimes result in the following output:
But sometimes it may also result in the following output:
So, I recommend you to use your variable in the
ViewWillAppear-method instead of theViewDidLoad-method.You should create your
UIView‘s and stuff in theViewDidLoad-method and adjust them to your test-variable in theViewWillAppear-method :).It’s a bit confusing, I know, and it took me a few hours to figure out why my variable wasn’t set already too.