【问题标题】:Adding two values in the strings together in ios在ios中将字符串中的两个值相加
【发布时间】:2012-11-01 08:03:04
【问题描述】:

我想将格式为“HH:mm:ss.SSS”的字符串中的值相加并再次显示。就我而言,它是添加不同的 timeString 来获取 currentString。我不确定正确的做法。我肯定做错了。我面临的问题是,当我单击停止按钮并再次单击开始时,它再次从 0 开始。我希望它从停止的同一个位置开始......

部分代码在这里:

-(void)updateTimer{
    currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    timeString=[dateFormatter stringFromDate:timerDate];
    currentString=[dateFormatter stringFromDate:timerDate];
    //double doubleOfString = [timeString doubleValue] + [difference doubleValue];
    //currentString = [NSString stringWithFormat:@"%f",doubleOfString];
    lbl.text = currentString;
}

完整的代码在这里:

Viewcontroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
    UILabel *lbl;
    NSTimer *stopTimer;
    NSDate *startDate,*currentDate;
    BOOL running,lap;
    UIButton *bttn;
    NSMutableArray *tableItems;
    NSString *timeString,*currentString,*difference;
    UITableView *tableview;
}

@property (strong,nonatomic) IBOutlet UILabel *lbl;
@property (strong,nonatomic) IBOutlet UIButton *bttn;
@property (strong,nonatomic) NSMutableArray *tableItems;
@property (strong,nonatomic) NSString *timeString;
@property (strong,nonatomic) IBOutlet UITableView *tableview;

-(IBAction)startPressed:(id)sender;
-(IBAction)resetPressed:(id)sender;

-(void)updateTimer;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize lbl,bttn,tableItems,timeString,tableview;

- (void)viewDidLoad
{
    [super viewDidLoad];
    lbl.text = @"00.00.00.000";
    running = FALSE;
    lap = FALSE;
    difference = @"0";
    startDate = [NSDate date];
    tableItems = [[NSMutableArray alloc] init];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)startPressed:(id)sender{
    if(!running){
        running = TRUE;
        lap = TRUE;
        [sender setTitle:@"Stop" forState:UIControlStateNormal];
        [bttn setTitle:@"Lap" forState:UIControlStateNormal];
        if (stopTimer == nil) {
            stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                         target:self
                                                       selector:@selector(updateTimer)
                                                       userInfo:nil
                                                        repeats:YES];
        }
    }else{
        running = FALSE;
        lap = FALSE;
        startDate = currentDate;
        difference = currentString;
        [sender setTitle:@"Start" forState:UIControlStateNormal];
        [bttn setTitle:@"Restart" forState:UIControlStateNormal];
        [stopTimer invalidate];
        stopTimer = nil;
    }

}
-(void)updateTimer{
    currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    timeString=[dateFormatter stringFromDate:timerDate];
    currentString=[dateFormatter stringFromDate:timerDate];
    double doubleOfString = [timeString doubleValue] + [difference doubleValue];
    //currentString = [NSString stringWithFormat:@"%f",doubleOfString];
    lbl.text = currentString;
}

-(IBAction)resetPressed:(id)sender{
    if (!lap) {
        [stopTimer invalidate];
        stopTimer = nil;
        tableItems = [[NSMutableArray alloc] init];
        startDate = [NSDate date];
        lbl.text = @"00.00.00.000";
        running = FALSE;
    }
    else{
        [tableItems insertObject:timeString atIndex:0];
        [tableview reloadData];
    }

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return tableItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //Step 1:Check whether if we can reuse a cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    //Step2: If there are no new cells to reuse,create a new one
    if(cell ==  nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        //UIView *v = [[UIView alloc] init];
        //v.backgroundColor = [UIColor redColor];
        //cell.selectedBackgroundView = v;
        //changing the radius of the corners
        //cell.layer.cornerRadius = 10;

    }

    //Step 3: Set the cell text content
    cell.textLabel.text = [tableItems objectAtIndex:indexPath.row];

    //Step 4: Return the row
    return cell;

}



@end

欢迎您提出任何其他更正建议...

【问题讨论】:

  • 需要添加timeString到差异...
  • 我面临的问题是,当我单击停止按钮并再次单击开始时,它再次从 0 开始。我希望它从停止的同一个位置开始。

标签: iphone ios nsstring nstimer nstimeinterval


【解决方案1】:

我建议不要将 math 应用于 strings

我的意思是:
“apple”和“Millenium Falcon”这两个词的总和是多少?没有任何意义,不是吗?

那么,当你有一个非常好的数字 (timeInterval) 时,你为什么要尝试形成两个字符串的 加法 - 一个非常适合对其执行代数运算的类型?

PS:
NSDateFormatter 实例希望被重用。将你的存储在实例变量中。

【讨论】:

    【解决方案2】:

    您的更新计时器功能将是:首先在.h文件中添加int counter。在 viewDidLoad counter = 0;

     -(void)updateTimer{
    
      counter ++;
      int hours = counter / 3600 ;
      int minutes = (counter / 60) - (hours * 60); 
      int seconds = counter - (hours * 3600) - (minutes * 60);
    
      lbl.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
    }
    

    【讨论】:

    • 您的 sharedInstance 来自哪里?顺便说一句,我的代码实际上是正确的..只需将差异加在一起...
    • 感谢其他解决方案,但它无助于解决我当前的问题..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    相关资源
    最近更新 更多