Problem opening new ViewController after UIImagePickerController

随声附和 提交于 2019-12-02 04:18:01

Implement viewDidAppear in your root view controller and based on member data decide to call showPhoto or not. The following example simply re-presents the UIImagePicker but any new modal view will work. viewDidAppear is called any time your root view controller’s view appears so you have to make sure the context is known when it is called. But it is the deterministic way to know that the modal view controller is gone.

- (IBAction) showPicker: (id) sender
{
    UIImagePickerController* picker = [[[UIImagePickerController alloc] init] autorelease];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    picker.allowsEditing = YES;

    [self presentModalViewController:picker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    imageChosen = YES;

    [self dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    imageChosen = NO;

    [self dismissModalViewControllerAnimated:YES];
}

- (void) viewDidAppear: (BOOL) animated
{
    if ( imageChosen )
    {
        [self showPicker: self];
    }
}

Another way to do this is to wrap the built-in dismissal animation with your own animation, and then catch the animationDidStop "event." This creates a composite animation, so when the built-in animation is done, your (empty) wrapper animation finishes you and alerts you that you're done.

This is slightly cleaner than the other answer here, IMO, as you don't need to keep a state variable or override viewDidAppear: (in my app, the view controller presenting the picker is quite a few objects removed from the utility code that handles picker management, and that would mean any view controller using my shared utility would have to override viewDidAppear:, or else fail to work):

-(void)dismissalAnimationDone:(NSString*)animationID 
                     finished:(BOOL)finished context:(void*)context
{
    UIImage* image = (UIImage*) context;
    // present controllers as you please
}

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    [UIView beginAnimations:@"dismissal wrapper" context:image];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(dismissalAnimationDone:finished:context:)];

    [self.delegate dismissModalViewControllerAnimated:YES];

    [UIView commitAnimations];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!