質問

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];
}
役に立ちましたか?

解決

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;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top