3

I just started getting into iPhone development. I have been mish-mashing tutorials and material from books in order to get my bearings straight. I come from a PHP and Java background... Objective-C is a bit quirky. But, I learn best by getting my feet wet.

Basically, I have these actions. getPhoto is bound to a couple of UIBarButtonItems in my view.

-(IBAction) getPhoto:(id) sender {
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if((UIBarButtonItem *) sender == choosePhoto) {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    } else {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }

    [self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
    theimageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}

My goal is to invoke the same action once the application launches, automatically opening the Camera. How would I go about this?

2
  • 1
    In my self appointed role as interface nazi I have to ask: Will your users expect the app to open to the camera? The first time they use it will it be readily apparent to them why the camera popped up? Unless your app is named "Take Picture" or something equally evocative, jumping straight the camera can cause confusion.
    – TechZen
    Feb 26, 2010 at 22:19
  • 1
    Also, the method is not bound to the actions. Binding has specific meaning in the Apple API and its important long term not to confuse it. If you think of the method as bound to the buttons your in for a world of confusion. Instead, the buttons are sending a message to the object in the form of action method call. The action in IB merely tells the button what object to target and what message to send.
    – TechZen
    Feb 26, 2010 at 22:22

1 Answer 1

3

EDIT:

As per this SO question you should actually place it in viewWillAppear or viewDidAppear


Add a similar method to the ApplicationDidFinishLaunching method in the app delegate.

Might be better to place the call in the ViewDidLoad of your root view controller

4
  • Like this? [self getPhoto]; Or how? Feb 26, 2010 at 21:21
  • Well yeah, you don't want to use sender from the app delegate though. You might need to alter your existing method or launch one similar since sender in this case will be the app delegate Feb 26, 2010 at 21:25
  • see edit and the answer from jm below. In this case the viewDidLoad method is probably more appropriate Feb 26, 2010 at 21:28
  • have you put UIImagePickerControllerDelegate in the header file of your app delegate? Feb 26, 2010 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.