解雇UIImagePickerController将UITabBarController重置为0索引 - iOS

时间:2016-05-29 10:56:08

标签: ios objective-c uitabbarcontroller uiimagepickercontroller presentmodalviewcontroller

我的UITabBarController中有4个标签,我在tabBar的第4个ViewController中显示UIImagePickerController。 (所有选项卡viewControllers都嵌入在UINavigationController

当我的UIImagePickerController从我的第四个标签中解雇时,无论是在选择图像还是取消选择器之后,它都会将我带回到我的第一个标签(0索引)。

它应该保留到我的第四个选项卡viewController。为什么会这样?我在代码中搜索了所有内容,但我没有提到tabBarController.setSelectedIndex = 0;

这是我提供UIImagePickerController并解雇它的代码:

- (void)takeNewPhotoFromCamera
{
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *controller = [[UIImagePickerController alloc] init];
        controller.sourceType = UIImagePickerControllerSourceTypeCamera;
        controller.allowsEditing = NO;
        //controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
        controller.delegate = self;
        [self presentViewController: controller animated: YES completion: nil];

    }
}

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

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];  
}

请帮忙。

谢谢!

1 个答案:

答案 0 :(得分:3)

通过将图片选择器的modalPresentationStyle设置为OverCurrentContext来找到解决方案:

UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
controller.allowsEditing = NO;
controller.delegate = self;
controller.modalPresentationStyle = UIModalPresentationOverCurrentContext; //This is the magical line
[self presentViewController: controller animated: YES completion: nil];