TWcode
TWcode

Reputation: 823

How to access NSUserdefault data in multiple view controllers?

I'm using multiple uiviewControllers to store information. I'm using NSUserDefaults to store the data. I have a UIVewController that will total everyting. How do I access the NSUserDefault data and how would I go about totaling it?

Upvotes: 0

Views: 584

Answers (1)

EmptyStack
EmptyStack

Reputation: 51374

You can use NSUserDefaults from any view controller. Just an example below.

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
int val1 = [[userDefaults valueForKey:@"Value1"] intValue];
int val2 = [[userDefaults valueForKey:@"Value2"] intValue];
int val3 = [[userDefaults valueForKey:@"Value3"] intValue];
int total = val1 + val2 + val3;

Upvotes: 1

Related Questions