在UIImagePickerController之后打开新的ViewController时出现问题 [英] Problem opening new ViewController after UIImagePickerController

查看:102
本文介绍了在UIImagePickerController之后打开新的ViewController时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的UIImagePickerController的委托函数返回didFinishPickingImage后立即打开一个新视图(UnprocessedPhotoViewController)。

I am trying to open up a new view (UnprocessedPhotoViewController) immediately after the delegate function for my UIImagePickerController returns a "didFinishPickingImage".

不幸的是,似乎我可以打开UIImagePickerController模式视图,或者切换到UnprocessedPhotoViewController作为模态,但不能同时按顺序切换。

Unfortunately, it appears that I can either open the UIImagePickerController modal view, or switch to the UnprocessedPhotoViewController as a modal, but not both sequentially.

在下面的代码中,按下按钮可激活pickPhoto IBAction。此代码成功激活UIImagePickerController。在用户选择图像之后,调用didFinishPickingImage委托函数,该函数将图像存储到变量,尝试关闭UIImagePickerController模式并在新模态中打开UnprocessedPhotoViewController。

In the code below, a button press activates the pickPhoto IBAction. This code activates the UIImagePickerController successfully. After the user selects an image, the didFinishPickingImage delegate function is called, which stores the image to a variable, attempts to close the UIImagePickerController modal and open the UnprocessedPhotoViewController in a new modal.

注意:如果我注释掉ImagePicker并直接运行showPhoto,则UnprocessedPhotoViewController会成功显示。此外,如果我创建一个新按钮来启动任一视图它成功,但我无法按顺序启动视图。我希望在用户选择图像后,将启动新视图,允许用户处理图像。

Note: If I comment out the ImagePicker and run "showPhoto" directly, the UnprocessedPhotoViewController shows successfully. Also, if I make a new button to launch either view it works successfully, but I am unable to launch the views sequentially. I would expect that after a user selected the image, the new view would be launched which would allow the user to process the image.

保证这一点的正确方法是什么ImagePicker模式关闭,然后打开UnprocessedPhotoViewController?

What is the correct way to guarantee that the ImagePicker modal closes and then to open the UnprocessedPhotoViewController?

谢谢!!!

代码:

- (IBAction)pickPhoto:(id)sender{

//TODO: To be replaced with the gallery control launching code

// Load Image Selection Code    
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:self.imgPicker animated:YES];

}



// Dummy function assumes you either picked (or took a picture =D) of Angie and moves you right to the unprocessed viewing screen.
- (void) showPhoto{

// Start new view controller
[self dismissModalViewControllerAnimated:YES];
UnprocessedPhotoViewController *upViewController = [[UnprocessedPhotoViewController alloc] initWithNibName:@"UnprocessedPhotoViewController" bundle:nil];
upViewController.imageView.image = selectedImage;
upViewController.delegate = self;
upViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:[upViewController animated:YES]];
[upViewController release];

}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img {
selectedImage = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[self dismissModalViewControllerAnimated:YES];

[self showPhoto];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}


推荐答案

在根视图中实现viewDidAppear控制器并基于成员数据决定是否调用showPhoto。以下示例只是重新呈现UIImagePicker,但任何新的模态视图都可以使用。每次出现根视图控制器的视图时都会调用viewDidAppear,因此您必须确保在调用上下文时已知该上下文。但这是确定模态视图控制器消失的确定性方法。

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];
    }
}

这篇关于在UIImagePickerController之后打开新的ViewController时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆