0

What I am trying to do is display a calculated variable from my FirstViewController.m file in a text field in my SecondViewController.m file. The simplest way I found to do this (although probably not the best) was by using NSUserDefaults. The problem I am encountering is that when using the app if I go back to the first tab and change the value of the variable, the text field in the second tab does not refresh to reflect this when I go back onto it. I want it to change automatically without the user having to press a button. Is there any way of doing this? Also a better way to access the variable from the second class would be very useful.

//FirstViewController.m

//Calculation of epleyInt using other text fields and pressing a button.

[epleyField setText:[NSString stringWithFormat:@"%i", epleyInt]];

NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
[settings setInteger:epleyInt forKey:@"epley"];
[settings synchronize];

and

//SecondViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
int epleyInt = [settings integerForKey:@"epley"];

[epley10Field setText:[NSString stringWithFormat:@"%i", epleyInt]];
}
Oli Walker
  • 11
  • 1
  • 3

1 Answers1

0

Managed to do it using singleton variables and moving the code to update the textfield to the viewDidAppear method.

I found this very helpful: Simple Passing of variables between classes in Xcode

Community
  • 1
  • 1
Oli Walker
  • 11
  • 1
  • 3