Andy A
Andy A

Reputation: 236

Programming a button press in one view controller, to load and save a cell in a different table view controller

In my application I have a number of view controllers, all of which have a favorite button on. When the favorite button is pressed, I want some information about the view controller to be saved in a separate table view in a different table view controller.

This is my first IOs app, and even after reading about Core Data, I'm at a bit of a loss as to how to go forwards.

How would I go about implementing such a feature?

Thanks for helping a novice out!

Cheers.

Upvotes: 0

Views: 123

Answers (1)

Saravana Vijay
Saravana Vijay

Reputation: 572

To store information locally you can use the NSUserDefaults class.

On the favorite button action method, save the viewcontroller name in an NSMutableArray using NSUserDefaults

    -(IBAction)btnFavouriteClick:(id)sender {

      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

      //Get favorited views array (new array will be created if it doesn't  exist)    
      NSMutableArray *favoriteviews = [defaults mutableArrayValueForKey:@"favorite_views_key"];

      //Add current view name to the array
      [favoriteviews addObject:@"viewcontroller_name"];

      //Add the array back in NSUserDefaults
      [defaults setObject:favoriteviews forKey:@"favorite_views_key"];

      //Save NSUserDefaults
      [defaults synchronize];
    }

You can retrieve the values using favorite_views_key and show them in the UITableView

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  

    //Get favorited views array
    NSMutableArray *favoriteviews = [defaults mutableArrayValueForKey:@"favorite_views_key"];

favoriteviews is array of all the favorited views.

Upvotes: 0

Related Questions