将 NSMutableArray 中的所有值相加并根据普通分段控制乘以值
Posted
技术标签:
【中文标题】将 NSMutableArray 中的所有值相加并根据普通分段控制乘以值【英文标题】:Add up all values in NSMutableArray and multiply by value depending on Plain Segmented Control 【发布时间】:2014-05-02 01:37:15 【问题描述】:我想要做的是在使用普通分段控件选择中等强度时将最新的 NSMutableArray 数组条目(使用 UITextField 输入)乘以 3,在选择剧烈时乘以 6,然后显示所有条目的总值乘法发生后的数组。例如。如果用户使用 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;
【问题讨论】:
当用户更改分段控件时,计数是否回到0?还是不管它是否乘以所有先前的值? 您的第二个视图控制器没有activities
属性。另外我不确定你为什么要添加到prepareForSegue
中的数组中。虽然你会有一个按钮来向数组添加一个活动,而另一个按钮来触发第二个视图控制器的 segue
不是@property NSMutableArray* 活动;对?是的,我不确定哈哈?我有它,所以一旦用户在 activityField 和 nameField 中输入值,然后按下提交,数组将被添加到 UITableView 并一次性将用户带到下一个视图控制器
您的@property 是正确的,但它在您的第一个视图控制器上。您的 prepareForSegue
正在尝试将其设置为您的第二个。听起来您可以使用单个视图来执行此操作 - 屏幕顶部的字段用于输入数据和下面的 tableView。然后,您将有一个 IBAction 方法链接到顶部的添加按钮,该按钮将数据添加到数组中。您的第一个视图控制器也将是您的表数据源(由活动数组驱动)
是的,我在两个视图上都需要它,但是因为一旦通过了这项修路工作,我还有更多要添加的内容。无论如何,您可以向我展示如何使用我目前拥有的代码来做到这一点吗?它之前将所需的数据传递给 tableview 我只是无法像最初的问题那样让它做下一点。头要爆炸了,我一直在转圈哈哈。非常感谢您迄今为止的帮助
【参考方案1】:
您的数组似乎包含任意字符串,而不是表示为字符串的整数(“10”的intValue
是10,但“中等强度:10 分钟”的intValue
是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;
【讨论】:
所以我已经通过我的代码来理解这一切,当我在 cell.textLabel.text = 行运行程序后尝试提交用户数据时收到一条线程 1 消息[self.activities objectAtIndex:indexPath.row];返回单元格;在 SeconViewController.m 中我无法解决? 日志说“无法识别的选择器发送到实例”[self.activities objectAtIndex:indexPath.row]
不返回字符串,而是返回MyActivity
。您需要使用MyActivity *activity=(MyActivity *)[self.activities objectAtIndex:indexPath.row]
,然后您可以使用activity.duration
和activity.intensity
属性构建一个字符串
甚至不确定如何处理未定义的 MyActivity
确保 #import "MyActivity.h" 在 SecondViewController.m 的顶部以上是关于将 NSMutableArray 中的所有值相加并根据普通分段控制乘以值的主要内容,如果未能解决你的问题,请参考以下文章
如何将存储在 NSMutableArray.. 中的值打印到 UITableViewcell
如何将CGContextAddArc(CGContextRef)中的所有点存储到NSMUtableArray