DrKey
DrKey

Reputation: 3495

iOS 7 - Unable to present UIImagePickerController on tabbed application

I'm working on a tabbed application which has 5 UITabBarItem. On the second UITabBarItem I would display the photo album using an UIImagePickerController, but the result is a strange black-white screen (I will post it at bottom).

Here is my SecondViewController.h:

#import <UIKit/UIKit.h>

@interface SecondViewController : UIImagePickerController  <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@end

and SecondViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

        UIAlertView *noLibraryAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                    message:@"No photo library!"
                                                                   delegate:nil
                                                          cancelButtonTitle:@"OK"
                                                          otherButtonTitles: nil];

        [noLibraryAlertView show];
    } else {
        self.delegate = self;
        self.allowsEditing = NO;
        self.navigationBarHidden = NO;
        self.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }
}

But when I select the second UITabBarItem the app shows this: enter image description here

Any ideas?

Thanks in advance.

Upvotes: 0

Views: 173

Answers (3)

DrKey
DrKey

Reputation: 3495

I'm sorry, it was a stupid problem in

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

It was an autocomplete mistake, but many thanks to all for informations about presentation and subclassing of UIImagePickerController.

Upvotes: 0

LombaX
LombaX

Reputation: 17364

Officially you can't subclass UIImagePickerController. However, assuming that you are using storyboard to instantiate SecondViewController, I found that changing the code like this would work:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        self.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        // other settings
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

        UIAlertView *noLibraryAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                     message:@"No photo library!"
                                                                    delegate:nil
                                                           cancelButtonTitle:@"OK"
                                                           otherButtonTitles: nil];

        [noLibraryAlertView show];
    }
}

Your code wasn't working correctly because you were setting the sourceType after the view was loaded.

Upvotes: 0

Breno Gazzola
Breno Gazzola

Reputation: 2162

According to apple's documentation a UIImagePicker must be presented modally:

Present the user interface. On iPhone or iPod touch, do this modally (full-screen) by calling the presentViewController:animated:completion: method of the currently active view controller, passing your configured image picker controller as the new view controller.

So it won't work in a tabbarcontroller or as a child of another controller.

Upvotes: 1

Related Questions