JND
JND

Reputation: 170

Attempt to present UIImagePickerController on UITabBarController while a presentation is in progress

I need help for resolving a warning error. I use a UIImagePickerController and it's showing when I tap on the third tab on my tabBar. When I cancel, I want to show the last tab before the selection of the camera, but when I dismiss my picker, I get a warning!!

This is my code and I don't understand why I get the warning...

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


- (void) showPickerController
{
    imagePicker = [[UIImagePickerController alloc] init];
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
    } else{
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Erreur !"
                                                          message:@"Votre iPhone n'a pas de Camera"
                                                         delegate:self
                                                cancelButtonTitle:@"Ok"
                                                otherButtonTitles:nil];
        [message show];
    }
    [self presentViewController:imagePicker animated:NO completion:nil];
}

#pragma mark - Camera Methods

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self.tabBarController dismissViewControllerAnimated:NO completion:^{
        if(self.tabBarController!=nil)
            [self.tabBarController setSelectedIndex:0];
    }];
}

Any idea?

Upvotes: 1

Views: 2132

Answers (2)

TylerP
TylerP

Reputation: 9829

In addition to danh's answer, add a BOOL property to your view controller and add a bit of code to your viewDidAppear: method:

@property (nonatomic) BOOL appeared;

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

    if (!self.appeared || [self isMovingToParentViewController] || [self isBeingPresented]) {
        self.appeared = YES;
        [self showPickerController];
    }
}

This will keep the UIImagePickerController from being immediately presented again after being dismissed.

Also, this isn't really relevant to the question, but you might want to change your showPickerController method a bit to prevent from showing an empty UIImagePickerController if there's no camera available:

- (void)showPickerController
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
        [self presentViewController:imagePicker animated:NO completion:nil];
    }
    else {
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Erreur !"
                                                          message:@"Votre iPhone n'a pas de Camera"
                                                         delegate:self
                                                cancelButtonTitle:@"Ok"
                                                otherButtonTitles:nil];
        [message show];
    }
}

Upvotes: 1

danh
danh

Reputation: 62676

Simple idea: change this to:

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self showPickerController];
}

Probably a better idea is to detect the source-type-available condition before beginning the transition to this vc, and instead present the picker vc instead.

Upvotes: 1

Related Questions