0

我有一个 viewController 从按钮操作加载我的 imagePickerController。当 imagePickerController 加载时,它应该显示两个带有预定数据的标签。一个是timerLabel,另一个是titleLabel。当 imagePickerController 首次加载时,两个标签都显示正确的数据,但片刻之后,timerLabel 数据消失了,但标签本身仍然存在。在我的 refreshLabel 方法中,NSLogged timerLabel 是正确的,但是当它到达 NSLog 的 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
    }
}
4

0 回答 0