在NSMutableArray中添加所有值,并根据Plain Segmented Control乘以值

时间:2014-05-02 01:37:15

标签: ios objective-c cocoa-touch uitableview nsmutablearray

我想要做的是将最新的NSMutableArray数组条目(使用UITextField输入)乘以3,当使用Plain Segmented Control选择中等强度时选择3,选择vigorous时选择6,然后显示所有条目的总值。乘法发生后的数组。例如。如果User使用Plain Segmented Control选择Moderate并在UITextField中输入120,我需要显示一个值360,并且该值随着更多条目的增加而递增。

到目前为止,我将数组值存储在如下表中,这样可以正常工作。

-(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;

}

}

我似乎无法乘以并输出值。我一直在玩像

这样的东西
if(styleseg.selectedSegmentIndex == 0){

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

       6x(what the user entered in duration)
}

一个循环试图将数组中的总值相加,只输出0.

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

我很难将两者混合在一起做我想做的事。任何帮助是极大的赞赏。

新编辑

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;
}

1 个答案:

答案 0 :(得分:0)

您的数组似乎包含任意字符串,而不是表示为字符串的整数(&{34; 10&#34;的intValue为10,而{&#34;中等强度的intValue为:10分钟&#34;是0)。您还要为活动的每个实例添加多个元素 - 活动描述和活动持续时间。

我会创建另一个对象类来封装活动 -

MyActivity.h

@interface MyActivity : NSObject

@property (copy,nonatomic) NSString *description;
@property int duration;
@property int intensity;

@end

创建一个UIButton并将其设置为事件内的doAddToArray。这将为每个新激活添加一个数组条目。分配新的MyActivity并在将其添加到数组之前设置适当的属性 -

在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;


}

然后在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

为简洁起见,我不会包含完整的SecondViewController.m,但您有许多地方使用activities self.activities

然后总计数组(无论你需要的地方) -

int total=0;
for (MyActivity *activity in self.activities)
{
    total+=activity.duration*activity.intensity;
}