2012-11-01 2 views
0

"HH : mm : ss.SSS"형식의 문자열에 값을 함께 추가하고 다시 표시하고 싶습니다. 내 경우에는 currentString을 얻기 위해 timeString을 차이점과 함께 추가하고있다. 나는 그것을하는 정확한 방법의 확실하지 않다. 나는 그것을 잘못하고있다. 내가 직면하는 문제는 내가 멈춤 버튼을 클릭하고 다시 시작을 클릭하면 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 

난 당신이 제안 할 수있는 다른 수정을 환영합니다 (3210) ...

+0

정확히 무엇이 필요합니까? –

+0

차이에 timeString을 추가해야합니다 ... – lakesh

+0

내가 직면하는 문제는 정지 버튼을 클릭하고 다시 시작을 클릭하면 다시 0에서 시작한다는 것입니다. 나는 그것이 왼쪽의 같은 지점에서부터 시작하고 싶습니다. off .. – lakesh

답변

1

에서 나는 친절 수학에 문자열 적용되지 건의 할 것입니다.

뜻 :
"apple"과 "Millenium Falcon"이란 단어의 합계는 얼마입니까? 말이 안되니?

따라서 두 개의 문자열로 구성된을 만들려고합니다. 대수 조작을 수행하기에 완벽하게 적합한 숫자 인 timeInterval입니다.

추신 :
NSDateFormatter 인스턴스를 다시 사용하고 싶습니다. 인스턴스 변수에 숨 깁니다.

1

귀하의 업데이트 타이머 기능은 다음과 같습니다 첫째 .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]; 
} 
+0

sharedInstance는 어디에서 제공 되나요? btw 내 코드가 실제로 정확합니다. 그냥 차이를 함께 추가 할 필요가 ... – lakesh

+0

수정 된 답변 확인 ... –

+0

다른 솔루션에 대한 감사하지만 내 현재 문제를 해결하는 데 도움이되지 않습니다 .. – lakesh