views:

45

answers:

1

I'm just playing around with a simple program that opens the camera. That's literally all that I want to do. I'm a beginner and I believe that I have the basics down in terms of UI management for the iPhone so I decided to give this one a whirl.

What I'm trying to do right now is...

- (BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions:(NSDictionary*) launchOptions
{
    UIImagePickerController * camera = [[UIImagePickerController alloc] init];
    camera.delegate = self;
    camera.sourceType = UIImagePickerControllerSourceTypeCamera;
    camera.allowsEditing = NO;
    camera.showsCameraControls = NO;
    [viewController presentModalViewController:camera animated:NO];

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

return YES:
}

So basically, initialize the camera, set some things and show it in the main view. I set the camera's delegate to self because this code is placed in the delegate class (and yes, the delegate class is conforming to UIImagePickerControllerDelegate && UINavigationControllerDelegate).

Main problem right now is that nothing is appearing on the screen. I have absolutely no idea what I'm doing wrong, especially since the program is building correctly with no errors or warnings...

Any help is appreciated! Thanks a lot :D

+2  A: 

You are trying to display the UIImagePickerController from your view controller before you'e added its view to the window. That won't work. It has to be presented from a view that is already displaying.

I'm not absolutely sure this will work as I haven't tried it, but switch the order of your code. Add your view controller to the window first:

- (BOOL) application:(UIApplication*) application 
             didFinishLaunchingWithOptions:(NSDictionary*) launchOptions
{
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    UIImagePickerController * camera = [[UIImagePickerController alloc] init];
    camera.delegate = self;
    camera.sourceType = UIImagePickerControllerSourceTypeCamera;
    camera.allowsEditing = NO;
    camera.showsCameraControls = NO;
    [viewController presentModalViewController:camera animated:NO];

    return YES:
}

If this doesn't work, then try moving your UIImagePickerController code to your -viewDidLoad of your root view controller (whatever viewController is in your code).

Matt Long