user6335453
user6335453

Reputation: 145

How to Save View Controller Data Every Time New Tab Bar Item is Selected?

I have set up a Tab Bar that controls some view controllers in my app. In my view controller CheckViewController, I have user entry-based data that I would like to save every time I am switching from CheckViewController to another view controller. The code I have currently only saves data the first time I switch from CheckViewController to another view controller using the tab bar. I would like to save data every single time CheckViewController is opened and switched to another view controller.

I am currently using:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tabBarController.delegate = self;

    //code

}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

    if (![viewController isEqual:self]) {

        if ([self.tabBarController.selectedViewController isEqual:self] ) {

            //save data

        }

    }

    return YES;

}

Suggestions are appreciated. Thanks!

Upvotes: 1

Views: 75

Answers (1)

Henson Fang
Henson Fang

Reputation: 1207

for saving small data,you can use NSUserDefault. here is an example for saving and reading data.

Save:

[[NSUserDefaults standardUserDefaults] setObject:aData forKey:aKey];
[[NSUserDefaults standardUserDefaults] synchronize];

Read:

[[NSUserDefaults standardUserDefaults] objectForKey:aKey];

Upvotes: 1

Related Questions