Question

What i want to do is multiply the latest NSMutableArray array entry (entered using a UITextField) by 3 when Moderate intensity is selected using the Plain Segmented Control and 6 when vigorous is selected and then display the total value of all entries in the array after the multiplications have occurred. E.g. If there User selects Moderate using the Plain Segmented Control and enters 120 in the UITextField, I need a value of 360 to be displayed and for that value to increment as more entries are made.

So far I'm storing the array values in a table like below which works fine.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"SecondViewControllerSegue"]) {
    SecondViewController *secondViewController
        = [segue destinationViewController];
    //secondViewController.infoRequest = self.nameField.text;

    NSString* style = (styleSeg.selectedSegmentIndex == 0) ? @"Moderate intensity for" : @"Vigourous intensity for";

    [activities addObject:[[NSString alloc]initWithFormat:@"Your activity: %@", self.activityField.text]];
    secondViewController.activities = activities;

    [activities addObject:[[NSString alloc]initWithFormat:@"%@: %@ minutes", style, self.nameField.text]];
    secondViewController.activities = activities;

}

}

I just can't seem to multiply and output the values. I've been playing around with something like

if(styleseg.selectedSegmentIndex == 0){

   3x(what the user entered in duration)
}
if(styleseg.selectedSegmentIndex == 1){

       6x(what the user entered in duration)
}

And a loop attempting to add up the total values in the array which is just outputting 0.

int result = 0;
for(int i=0;i<[activities count];i++)
result += [[activities objectAtIndex:i] intValue];
NSLog(@"result = %d", result);

I'm just having trouble blending the two together to do what I want. Any help is greatly appreciated.

NEW EDIT

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    IBOutlet UIView *nameView;
    IBOutlet UITextField *nameField;
    IBOutlet UITextField *activityField;
    IBOutlet UISegmentedControl *styleSeg;
}

@property UIView *nameView;
@property UITextField *nameField;
@property UITextField *activityField;
@property (strong,nonatomic) NSMutableArray *activities;

- (IBAction)submitButtonTapped:(id)sender;
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;

@end

ViewController.m

#import "ViewController.h"
#import "SecondViewController.h"
#import "MyActivity.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize nameView;
@synthesize nameField;
@synthesize activityField;
@synthesize activities;

- (void)viewDidLoad
{
    [super viewDidLoad];
    activities  = [[NSMutableArray alloc] init];
    //activityName  = [[NSMutableArray alloc] init];
}

-(void)viewWillAppear:(BOOL)animated
{
    self.nameField.text = @"";
    self.activityField.text = @"";
    styleSeg.selectedSegmentIndex = 0;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)submitButtonTapped:(id)sender {

    NSLog(@"The submit button was clicked.");
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"SecondViewControllerSegue"]) {
        SecondViewController *secondViewController
            = [segue destinationViewController];
        secondViewController.infoRequest = self.nameField.text;

       /* This was my initial code as in the intial question

        NSString* style = (styleSeg.selectedSegmentIndex == 0) ? @"Moderate intensity for" : @"Vigourous intensity for";

        [activities addObject:[[NSString alloc]initWithFormat:@"Your activity: %@", self.activityField.text]];
        secondViewController.activities = activities;

        [activities addObject:[[NSString alloc]initWithFormat:@"%@: %@ minutes", style, self.nameField.text]];
        secondViewController.activities = activities;

         */

        // New code
        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];
        secondViewController.activities = activities;


    }
}
@end

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController  <UITableViewDataSource, UITableViewDelegate>
{
    IBOutlet UIView *secondView;
    IBOutlet UILabel *nameLabel;
}

@property IBOutlet UITableView *activityTableView;
@property NSMutableArray* activities;
//@property NSMutableArray* activityName;

@property UIView *secondView;
@property UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel2;

@property id infoRequest;

-(IBAction)goBack:(id)sender;

@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize secondView;
@synthesize nameLabel;
@synthesize nameLabel2;
@synthesize activities;
@synthesize infoRequest;
@synthesize activityTableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.nameLabel.text = [self.infoRequest description];
    self.nameLabel2.text = [self.infoRequest description];
    // activities = [[NSArray alloc] init];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)goBack:(id)sender
{
    UINavigationController* parent = (UINavigationController*)[self parentViewController];
    [parent popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Number of rows is the number of time zones in the region for the specified section.
    return [activities count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellReuseIdentifier = @"CellReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:cellReuseIdentifier];
    }

    NSString* s = [activities objectAtIndex:indexPath.row];
    cell.textLabel.text = s;
    return cell;
}
Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top