Your array seems to hold arbitrary strings, not integers represented as strings (the intValue
of "10" is 10, but the intValue
of "Moderate intensity for: 10 minutes" is 0). Also you are adding multiple elements for each instance of an activity - the activity description and the activity duration.
I would create another object class to encapsulate the activity -
MyActivity.h
@interface MyActivity : NSObject
@property (copy,nonatomic) NSString *description;
@property int duration;
@property int intensity;
@end
Create a UIButton and set it's touch up inside event to doAddToArray
. This will add an entry to the array for each new activate. Allocate a new MyActivity
and set the appropriate properties before adding it to the array -
In ViewController.m
-(IBAction)doAddToArray:(id)sender {
MyActivity *activity=[[MyActivity alloc]init];
activity.description=self.activityField.text;
activity.duration=[self.nameField.text intValue];
activity.intensity=(styleSeg.selectedSegmentIndex == 0) ? 3:6;
[self.activities addObject:activity];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"SecondViewControllerSegue"]) {
SecondViewController *secondViewController
= [segue destinationViewController];
secondViewController.activities = activities;
}
Then in SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
IBOutlet UIView *secondView;
IBOutlet UILabel *nameLabel;
}
@property IBOutlet UITableView *activityTableView;
@property UIView *secondView;
@property UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel2;
@property (weak, nonatomic) NSArray *activities; // This is used to provide content to the UITableView
@property id infoRequest;
-(IBAction)goBack:(id)sender;
@end
For brevity I won't include the full SecondViewController.m, but you have a number of places where you use activities
that should be self.activities
Then to total the array (wherever you need to)-
int total=0;
for (MyActivity *activity in self.activities)
{
total+=activity.duration*activity.intensity;
}