标签中显示的NSTimer然后消失了吗?

时间:2013-09-10 02:25:34

标签: ios nstimer

我有一个viewController,它从一个按钮动作加载我的imagePickerController。当imagePickerController加载时,它应该显示带有覆盖图的两个带有预定数据的标签。一个是timerLabel,另一个是titleLabel。当imagePickerController首次加载时,两个标签都显示正确的数据,但仅在片刻之后,timerLabel数据消失,但标签本身仍然存在。在我的refreshLabel方法中,NSLogged timerLabel是正确的,但当它到达NSLog for startDate和timeLeft时,它返回(null)。有任何想法吗?谢谢!

    - (IBAction)startCamera:(id)sender {
        if (self.image == nil &&  [self.videoFilePath length] == 0) {
            self.imagePickerController = [[UIImagePickerController alloc] init];
            self.imagePickerController.delegate = self;
            self.imagePickerController.allowsEditing = NO;
            self.imagePickerController.videoMaximumDuration = 10;

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            }
            else {
                self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            }
            self.imagePickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePickerController.sourceType];
            [self presentViewController:self.imagePickerController animated:NO completion:nil];}

        [[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil];
        self.overlayView.frame = CGRectMake(160,8,0,0);
        self.imagePickerController.cameraOverlayView = self.overlayView;

        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *deadline = [NSString stringWithFormat:@"%@/deadline.txt",
                              documentsDirectory];
        NSString *name = [NSString stringWithFormat:@"%@/name.txt",
                               documentsDirectory];
        NSError *fileError;
        titleLabel.text = [NSString stringWithContentsOfFile:name
                                                    encoding:NSASCIIStringEncoding
                                                       error:&fileError];
        timerLabel.text = [NSString stringWithContentsOfFile:deadline
                                                    encoding:NSASCIIStringEncoding
                                                       error:&fileError];
        if(fileError.code == 0){
            NSLog(@"deadline.txt was read successfully with these contents: %@,",
                  timerLabel.text);
            NSLog(@"name.txt was read successfully with these contents: %@,",
                  titleLabel.text);}


     NSLog(@"timer label %@", timerLabel.text);

    [NSTimer scheduledTimerWithTimeInterval:1
                                     target:self
                                   selector:@selector(refreshLabel)
                                   userInfo:nil
                                    repeats:YES];
}

-(void)refreshLabel {
    NSLog(@"timer label %@", timerLabel.text);

    NSDate *startDate = [self.formatter dateFromString:timerLabel.text];
    NSDate *timeLeft = [startDate dateByAddingTimeInterval:-1];

    NSLog(@"start time %@",startDate);
    NSLog(@"time left %@",timeLeft);

    NSTimeInterval totalCountdownInterval =1;
    NSTimeInterval elapsedTime = [timeLeft timeIntervalSinceNow];
    NSTimeInterval remainingTime = totalCountdownInterval - elapsedTime;

    self.timerLabel.text = [self.formatter stringFromDate:timeLeft];

    if (remainingTime <= 0.0) {
        //dismiss controller and set to homecontroller at tabBar index 1
    }
}

0 个答案:

没有答案