呈现UIImagePickerController会删除当前控制器上的NavigationBar

时间:2012-11-27 16:02:48

标签: iphone ios uinavigationcontroller uiimagepickercontroller

我有一个带有多个控制器的标签栏应用程序。有些是UINavigationControllers,有些只是简单UIViewControllers。 中央按钮显示UIImagePickerController以允许用户拍照。 当我从简单的UIViewControllers呈现图像选择器时,它可以正常工作。 但是当我在UINavigationController当前为selectedViewController时呈现它时,选择器的代码会移除控制器的UINavigationBar

我用UINavigationControllers读取时,模态视图必须来自navigationController,但它也不起作用。 以下是一堆代码:

    UIImagePickerController* picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    if (TARGET_IPHONE_SIMULATOR)
    {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }
    else
    {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        else
        {
            UIAlertView* alert = [[UIAlertView alloc]
                                  initWithTitle:@"Erreur"
                                  message:@"Pour pouvoir poster des photos, votre device doit posséder un appareil photo"
                                  delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
            [alert show];
            return;
        }
    }
    if ([self.selectedViewController respondsToSelector:@selector(presentViewController:animated:completion:)])
    {
        [self.selectedViewController presentViewController:picker animated:YES completion:nil];
    }
    else
    {
        [self.selectedViewController presentModalViewController:picker animated:YES];
    }

沮丧:

[self.selectedViewController dismissModalViewControllerAnimated:YES];

有没有人有这种奇怪的行为(猜测它与UIImagePickerController委托必须实施UINavigationControllerDelegate)并知道如何解决这个问题有关? 感谢。

2 个答案:

答案 0 :(得分:1)

您可以使用此

重定向到pickerController
[self presentViewController:picker animated:YES completion:^{}];  

及其委托方法

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    NSData *dataImage = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1);
    [picker dismissViewControllerAnimated:YES completion:^{}];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}  

确保您未在​​代码中设置任何setHidden

答案 1 :(得分:1)

这实际上非常简单:在viewWillAppear中,我将隐藏属性设置为NO,并在viewWillDisappear中将其设置为YES。问题是:当呈现模态控制器时,调用了viewWillDisappear。但是当它被解雇时,viewWillAppear没有被调用......不是真正的预期行为,但仍然。谢谢你们。