각 셀에 버튼을 위치시키고,


각 버튼에는 이벤트를 달아 놓는다


셀에 위치한 시작버튼을 눌렀을때,


타이머가 작동할 수 있는 이벤트가 실행된다




=========================================================================


    [cellButton setTitle:@"시작" forState:0];

    

    [cellButton addTarget:self action:@selector(cellButtonClicked:) forControlEvents:UIControlEventTouchUpInside];


=========================================================================




버튼이 눌렸다면, 이벤트가 발생이 되고


이벤트 발생시에, 각 셀에 대한 타이머를 작동시킨다



=========================================================================


-(void)cellButtonClicked:(id)sender

{

    int selectedTag = (int)[sender tag];


    NSMutableArray *info = [[NSMutableArray alloc] init];

    NSNumber *cost = [[NSNumber alloc] initWithInt:selectedTag];

    [info addObject:cost];

    

    // 타이머 셋팅 시작 - 개별 선택된 셀의 타이머가 따로 작동한다

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod:) userInfo:info repeats:YES];

    

}


=========================================================================




타이머는 또 다른 메서드를 콜하게 된다


해당 메서드에서는 시간을 1초씩 감소시키는 작업을 하고,


타이머 종료시에 알람을 메시지 창으로 띄우는 역할을 한다




=========================================================================


-(void) timerMethod:(NSTimer *)timer

{

    NSNumber *cost = [[timer userInfo] objectAtIndex:0];

    

    int theTag = [cost intValue];    


    NSInteger a = [dataArray[theTag][1] integerValue];

    a = a - 1;

    

    NSString *temp = [NSString stringWithFormat:@"%ld", (long)a];

    

    // 시간값을 바꾸어 준다

    [[dataArray objectAtIndex:theTag] replaceObjectAtIndex:1 withObject:temp];


    

    // 미리 정의 데이터 파일로 아카이브

    [NSKeyedArchiver archiveRootObject:dataArray toFile:dataFilePath];

    

    

    // 타이머 종료 , 알람메시지를 띄운다

    if( 0 == [dataArray[theTag][1] integerValue])

    {

        NSString *temp2 = [NSString stringWithFormat:@"%d", theTag];

        UIAlertView *AlrmView = [[UIAlertView alloc] initWithTitle:@"알람시계" message:temp2 delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"") otherButtonTitles:nil];

        

        [AlrmView show];

        

        // 타이머를 종료한다

        [timer invalidate];

    }

    

    // 화면 재로드

    [self ReloadRecordList];


}


=========================================================================



Posted by 스타켄지니어
,