开发者

Setting a timer from a UITableViewCell button

I want to set a Time with a UIButton in a UITableViewCell. It is working when I click one bu开发者_如何学Gotton, but when I click 2 or more buttons the timer will not stop.

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    tabletTextLabel.text=[appDelegate.tabletArray objectAtIndex:indexPath.row];
    noOfTabletTextLabel.text=[appDelegate.noOfTabletsArray objectAtIndex:indexPath.row];



[cellButton setImage:[appDelegate.imageArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    [cellButton addTarget:self action:@selector(CellButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    cellButton.tag = indexPath.row;


// cell.accessoryView = cellButton;
    [cell.contentView addSubview:presTextLabel];
    [cell.contentView addSubview:tabletTextLabel];
    [cell.contentView addSubview:noOfTabletTextLabel];
    [cell.contentView addSubview:cellButton];
        return cell;

}

 -(void)CellButtonClick:(id)sender
    {

        UIButton *button = (UIButton *)sender;
        NSLog(@"%d", button.tag); 

        [self StartTimer];

    }



 -(void)StartTimer
    {
        timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(SetTabletAlert) userInfo:nil repeats:YES];
    }



  -(void)SetTabletAlert
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Reminder" 
            message:@"Take Tablets" 
            delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }



 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
    {  
         NSString *title = [alertView buttonTitleAtIndex:buttonIndex];  

        if([title isEqualToString:@"ok"])  
        {  
            if(timer)
            {
                [timer invalidate];
                timer=nil;
            }
        }   
    }


In startTimer you could check if a timer is already running and invalidate it before starting a new one.

if (timer) [timer invalidate];
timer = nil;
timer = [NSTimer scheduledTimerWithTimeInterval:....];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜