doc92606
doc92606

Reputation: 701

Sending Data from 2 View Controllers to One View Controller

I am coding a Calorie Tracker app in which I have 3 view controllers. They are all hooked up to a tab bar controller in the storyboard.

Essentially what I want to do is to take the data from my Exercise view Controller and My Diet View Controller and display it on my Home View Controller. This is my code for viewDidLoad in my HomeViewController.m

    //Referencing both view controllers
MainViewController *mainViewController = [[MainViewController alloc] init];
ExerciseViewController *exerciseViewController = [[ExerciseViewController alloc] init];

//Doing the math
int totalCalsConsumed = mainViewController.totalLabel.text.intValue;
int totalCalsBurned = exerciseViewController.totalCalsBurned.text.intValue;
int totalAmountOfCalories = totalCalsConsumed - totalCalsBurned;

//display the data
NSString *totalAmtCalsText = [NSString stringWithFormat:@"%i", totalAmountOfCalories];
totalAmtOfCals.text = totalAmtCalsText;

Also, I cannot pass any data with segues because all my view controllers are hooked up to a tab bar, and there is no prepareForSegue method for tab bars.

All help is appreciated, and I would also like to know if I DESPERATELY HAVE TO use Core Date for this dilemma. For now I'm trying to dodge Core Date for it is a very advanced topic that I will touch upon in the future, but If I MUST use Core Data for this app I'll figure something out.

Upvotes: 0

Views: 93

Answers (3)

Zeeshan
Zeeshan

Reputation: 4244

Key value Coding , NSNotificationCentre and Delegates are preferred. But NSNotificationCentre is easiest in your case.

The UIViewController Home View Controller must add observer like this : Init :

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodToUpdate:) name:@"UPDATE_ME" object:(id)anyObject];

In delloc method :

[[NSNotificationCenter defaultCenter] removeObserver:self];

Post it from other 2 classes like on any UIButton action:

[[NSNotificationCenter defaultCenter] postNotificationName:@"UPDATE_ME" object:(id)anyObject];

Advantage of NSNotificationCentre is that they can add observers in multiple classes ..

Upvotes: 0

Pavan
Pavan

Reputation: 18498

Due to your strong desire to avoid Core Data - which I feel should be considered - I can provide a quick, perhaps dirty (maybe not) solution so that you can have data accessible through out your viewControllers.

Based on what youre doing it seems as though you want to access data from your tabbar view controllers.

One way of doing this is to have a data model in one single place which can be accessible by other view controllers. Like a server with a webservice or have a singleton that keeps your data in one place.

In your case, you want a quick solution, you can do this for now which works, as long as all the ViewControllers mentioned are in your tabBar

//Create variables
int totalCalsConsumed;
int totalCalsBurned;
int totalAmountOfCalories;

//All your view controllers are accessible from your tabBar like so
NSArray *myViewControllers = self.tabBarController.viewControllers;

//We iterate through the view controllers in the tabBar
for(int i = 0; i < [myViewControllers count]; i++){
    //This is the current view controller in the for loop execution
    id currentVC = [myViewControllers objectAtIndex:i];

    //Check if the currentVC variable is of type MainViewController
    if([currentVC isKindOfClass:[MainViewController class]]{
        //lets access the totalLabel property and store in variable
        totalCalsConsumed = ((MainViewController *)currentVC).totalLabel.text.intValue;
    }
    //Check if the currentVC variable is of type ExerciseViewController
    else if([currentVC isKindOfClass:[ExerciseViewController class]]){
        //if so lets now access the totalCalsBurned property and store in variable
        totalCalsBurned = ((ExerciseViewController *)currentVC).totalCalsBurned.text.intValue;
    }
}

//Doing the math
totalAmountOfCalories = totalCalsConsumed - totalCalsBurned;

//display the data
NSString *totalAmtCalsText = [NSString stringWithFormat:@"%d", totalAmountOfCalories];
totalAmtOfCals.text = totalAmtCalsText;

I haven't tested this as I just wrote this now, but it should be good from the get go. Code commenting is there for you understand whats going on. Pretty straight forward.

tip for you mate since youre developing a fitness app In the future, I do recommend something more persistent, where by data isnt lost if the application has quit, especially since you're creating a fitness app. I currently am using the BodyBuilding fitness app in my personal time, and have been frustrated at times when data was lost during my work out (sets information, exercises done with total reps for each set) only because the developers never bothered to store my data somewhere whilst I was doing my exercise, in case for when my battery died out.

Upvotes: 1

Wolfgang Schreurs
Wolfgang Schreurs

Reputation: 11834

You could choose 1 of several approaches:

  1. You could create a Singleton and store/read all persistent data from this singleton. Singletons should be used with care though.
  2. You could use NSNotificationCenter to send out NSNotifications whenever some value changes. UIViewControllers can register to receive NSNotifications.
  3. You could store persistent data on the file system using CoreData or some other technology like NSUserDefaults or NSKeyedArchiver / NSKeyedUnarchiver.
  4. Perhaps a dependency injection method could be used as well. Creating an object in the app delegate and passing it to all related UIViewControllers. Use this object in the same way you'd use the singleton I mentioned as option 1.

There's probably more options that I can't remember at this current moment.

Upvotes: 0

Related Questions