dismissModalViewController无法按预期工作

时间:2010-07-02 17:18:43

标签: iphone uiimagepickercontroller

我有一个问题,我正在帮助别人可以解释一下。我确信这是非常简单的事情,但我只是看不到它,并且真的可以使用额外的一组眼睛来帮助找到这个bug。

我正在使用的应用程序使用UIImagePickerController来呈现隐藏了相机用户界面的相机预览。此预览实现了cameraOverlay视图,其中包含几个按钮以执行我所需的任务,其中一个是关闭相机预览。

出于某种原因,在叠加视图中按钮调用时,关闭摄像机视图的功能将不起作用。我可以NSLog该函数,并看到它实际上是由按钮调用,但只是不允许dismissModalViewController工作。我已经通过在加载后几秒钟让NSTimer调用它来测试该功能,并且它工作正常,只是当我从cameraOverlay中的按钮调用它时。

我的viewController(CameraTestViewController.m)按下按钮调用UIImagePicker,如下所示。

-(IBAction) getPhoto:(id) sender {


    OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
    picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    picker.cameraOverlayView = overlay;
    picker.showsCameraControls = NO;
    picker.toolbarHidden = YES;
    [self presentModalViewController:picker animated:NO];
    //[self performSelector:@selector(doCancel) withObject:nil afterDelay:10];

}

我在viewController的其他地方使用此方法关闭相机视图

-(void)doCancel
{   
    NSLog (@"doCancel method has been called");
    [picker dismissModalViewControllerAnimated:YES];
}

然后在我的OverlayView.m类中,我设置了所需的按钮并提供了一个调用模态视图控制器的方法。

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Clear the background of the overlay:
        self.opaque = NO;
        self.backgroundColor = [UIColor clearColor];

                  ...

myButton04 = [UIButton buttonWithType:UIButtonTypeCustom];
        myButton04.frame = CGRectMake(10, 447, 44, 13);
        [myButton04 setTitle:@"" forState:UIControlStateNormal];
        [myButton04 addTarget:self action:@selector(scanButton05TouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [myButton04 setImage:[UIImage imageNamed:@"camControlsBack.png"] forState:UIControlStateNormal];
        [self addSubview:myButton04];

                  ...

    }

    return self;

}


- (void) scanButton05TouchUpInside {

    NSLog(@"BACK BUTTON HAS BEEN PRESSED");
    CameraTestViewController *baseViewController = [[CameraTestViewController alloc] init];
    [baseViewController doCancel];

}

对此的任何帮助都将非常感激。我厌倦了一个紫色的额头从头部撞到墙上。 :酷:

谢谢!

2 个答案:

答案 0 :(得分:1)

使用以下代码:

[[[self presentingViewController] presentingViewController]  dismissModalViewControllerAnimated:YES];

答案 1 :(得分:0)

我解雇我的modalViewController的方法是调用它的父:

[[picker parentViewController] dismissModalViewControllerAnimated:YES];