1

When my iPhone 5 app starts up I present no view controller because of the following:

- (void) viewDidAppear:(BOOL)animated {
}

The first thing the user will do is touch a button that is on the initial screen that opens up a menu for retrieving a picture either from the user's photo library, camera, or a picture of obama. The method that is called when that button is touched by the user is:

- (IBAction) pickImage {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Image Source"
    delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil
    otherButtonTitles:@"Photo Library", @"Camera", @"Obama", nil];
    [actionSheet showInView:scrollView];
    [actionSheet release];
}

Now the actionSheet eventually calls:

[self presentViewController:imagePicker animated:TRUE completion:nil]; 

For the iPhone 5 everything works and the user is given 3 options with the presentViewController. After the user picks an image then I call:

[[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];

and the View Controller goes away. Now what I WOULD LIKE TO DO IS make the view controller appear initially so the user doesn't have to touch the button connected to pickImage by adding a line to the viewDidAppear method like so:

- (void) viewDidAppear:(BOOL)animated {
    [self pickImage]; // makes the image picker pop up when app intializes
}

HOWEVER, when I test this, the imagePicker automatically appears like I expected but then after I finish picking my image and dismiss it, it reappears after I have chosen my image. HOW CAN I FIX THIS???

The following is the class I am talking about: MainViewController.h FILE: http://db.tt/Vgm3w0gs MainViewController.m FILE: http://db.tt/uN8YdNGN

Or you can just look at the following relevant methods:

- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {

switch (buttonIndex) {
    case 0: { //photo library
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:imagePicker animated:TRUE completion:nil];               
        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Photo library is empty or unavailable" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
        }
        break;
    }
            case 1: //camera option has it's view controller dismissed after image is taken. The rest of ActionSheet doesn't really matter...

HERE is the method that dismisses the ViewController except when imagePicker is called from within viewDidAppear. QUESTION: How can I get the viewController to dismiss when imagePicker is called from within viewDidAppear?

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// loads image into main screen
[self loadImage:[info objectForKey:UIImagePickerControllerOriginalImage]];

    [[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}   [picker release];
}
letter Q
  • 14,735
  • 33
  • 79
  • 118
  • I tried to reproduce your problem from using a subset of your files but it was working fine on a iphone simulator, is your problem only on iPad or both ? – Gomino Jul 22 '13 at 10:04
  • @gomino James already answered my question but thanks for the help! – letter Q Jul 22 '13 at 16:08
  • Ok no problem, so what are you waiting for rewarding the bounty ? – Gomino Jul 22 '13 at 16:28

1 Answers1

2

Your current problem seems to be that you want pickImage to be called when the view first appears, but not when it reappears as a result of a popup window closing.

One possibilty would be to move the pickImage call from the viewDidAppear callback into the viewDidLoad callback.

However, if that is being called too soon, the other option would be to add a boolean variable that you check in viewDidAppear to make sure that pickImage is only called once.

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (firstAppearance) {
        firstAppearance = NO;
        [self pickImage];
    }
}

And set that boolean to true in the viewDidLoad callback.

- (void) viewDidLoad {
    [super viewDidLoad];
    firstAppearance = YES;
}

Obviously you would also need to declare the bool in your header file somewhere.

BOOL firstAppearance;
James Holderness
  • 22,721
  • 2
  • 40
  • 52
  • After using the camera, the image picker dismisses fine. The problem is that after picking an image from the photo library, the image picker code that dismisses it is run but then the image picker reappears and I do not understand why. – letter Q Jul 15 '13 at 18:46
  • @QuinnLiu On an iPhone at least, the code should be the same - it's just a different sourceType. But given that they're different controls, it wouldn't be a huge surprise if the dismiss code you were using worked for the one but not the other. Have you actually tried the code I suggested (also assuming you present the interface with `presentModalViewController`)? – James Holderness Jul 15 '13 at 19:03
  • Could you please provide your .h file? –  Jul 15 '13 at 19:22
  • @JamesHolderness Unfortunately it did not work but thanks for the reply. – letter Q Jul 15 '13 at 19:47
  • @YatinSaraiya I have put it the original .h and .m file in the question and here: http://db.tt/uN8YdNGN (.m file) and http://db.tt/Vgm3w0gs (.h file) – letter Q Jul 15 '13 at 19:53
  • @QuinnLiu Just realised `dismissModalViewControllerAnimated` is deprecated on iOS6 and you should be using `dismissViewControllerAnimated`. I know you said you tried that, but have you tried it in combination with `presentingViewController`, i.e. `[[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil]`. – James Holderness Jul 15 '13 at 20:35
  • @QuinnLiu If that fails, I have one last suggestion. Try downloading [Apple's sample app](https://developer.apple.com/library/IOs/#samplecode/PhotoPicker/Introduction/Intro.html). If you can get that to work, then slowly start converting the code to match your implementation until something breaks. If you can't even get that sample code to work, then I give up. :) – James Holderness Jul 15 '13 at 20:50
  • @QuinnLiu you are explicitly calling a reload of the imagePicker as in the edits to the post above. Removing that call will fix your Cancel problem at least. –  Jul 15 '13 at 22:47
  • @YatinSaraiya I changed up the question as to be easier to understand the flow of the program and what I want. – letter Q Jul 15 '13 at 23:34
  • @QuinnLiu I've updated my answer. I think that should solve your current problem if I've understood the issue correctly. – James Holderness Jul 16 '13 at 00:02