带有UIAlertView的imagePickerControllerDidCancel无法正常工作

时间:2013-09-09 00:58:25

标签: ios alertview

我正在尝试使用包含两个标签的自定义叠加层来呈现相机视图控制器。我无法让我的imagePickerControllerDidCancel方法和alertView正常工作。系统正在向NSLog打印正确的信息,但我写的代码行没有做任何事情,当用户点击“返回”(取消alertView并返回到imagePicker)时,摄像机会再次显示,但是拍摄照片按钮和取消按钮不再可访问。它们仍然可以看到但没有被拍打。当用户点击索引1处的按钮(离开)时,我希望关闭相机并将用户带回我的homeViewController,它位于tabBarController索引:1。此外,当相机首次加载时,timerLabel会显示正确的数据,但会很快消失。我觉得它与我的refreshLabel方法有关,但同样,我不清楚我在哪里出错了。这些东西都不适用于我,我对编程世界仍然很陌生,所以对这些问题的帮助将不胜感激。谢谢!

    #import "CameraViewController.h"
    #import "FriendsViewController.h"
    #import <mobileCoreServices/UTCoreTypes.h>

    @interface CameraViewController ()

    @end

    @implementation CameraViewController
    @synthesize  titleLabel;
    @synthesize timerLabel;

    - (void)viewDidLoad
    {   [super viewDidLoad];

        self.recipients = [[NSMutableArray alloc] init];
    }


    - (void)viewWillAppear:(BOOL)animated {
            [super viewWillAppear:animated];

        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;}

                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);}

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

    -(void)refreshLabel;{
        self.formatter = [NSDateFormatter new];
        [self.formatter setDateFormat:@"dd:hh:mm:ss"];

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

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

        if (remainingTime <= 0.0) {
            //dismiss controller and set to homecontroller at tabBar index 1
        }
        self.timerLabel.text = [self.formatter stringFromDate:timeLeft];
    }



    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Are you Sure?"
                                                            message:@"you can't come back"
                                                           delegate:self cancelButtonTitle:@"Back" otherButtonTitles:@"Yes", nil];
        [alertView show];
    }

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        if (buttonIndex==1) {
            [self.imagePickerController dismissViewControllerAnimated:NO completion:nil];
            [self.tabBarController setSelectedIndex:1];
            NSLog(@"Leave clicked");
    }
        else {
            [self reset];
            NSLog(@"cancel clicked");
        }
    }
- (void)reset {
    self.image = nil;
    self.videoFilePath = nil;
}

0 个答案:

没有答案