简体   繁体   English

当关闭iOS 8时ImagePicker删除视图

[英]ImagePicker removing views when dismissed iOS 8

My app is structured with a central view controller that adds 3 to 4 subviews to the main view. 我的应用程序由一个中央视图控制器构成,该控制器将3到4个子视图添加到主视图。 In one of the added view controllers I present a camera to the user. 在添加的视图控制器之一中,我向用户展示了一个摄像头。 When I dismiss the image picker, every subview except the one in which I present the camera (the view controller) disappears. 当我关闭图像选择器时,除我要展示相机的那个子视图(视图控制器)之外的所有子视图都消失了。 I think it might be related to how the app is structured and the view stack. 我认为这可能与应用程序的结构和视图堆栈有关。 The app works fine when running iOS 8 on an iPhone and iOS 7 on an iPad. 当在iPhone上运行iOS 8和在iPad上运行iOS 7时,该应用程序运行良好。 I am having this issue only when I am running iOS 8 on the iPad. 仅当我在iPad上运行iOS 8时,才遇到此问题。 I made sure the code followed the apple documentation on how to present and dismiss the image picker. 我确保代码遵循apple文档中有关如何显示和关闭图像选择器的内容。 Here is the code used to present the image picker. 这是用于呈现图像选择器的代码。

 -(IBAction)photoButtonPressed:(id)sender
{

    // Prompt for camera or photo library
    // Retrieve image and set it as this button's default image
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];

        imagePicker.delegate = self;

        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                  (NSString *) kUTTypeImage, nil];

        imagePicker.allowsEditing = NO;
        imagePicker.modalPresentationStyle = UIModalPresentationFullScreen; //play around with this

       [self presentViewController:imagePicker animated:YES completion:nil];

    } else {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Camera not found" message:@"There is no camera available on this device." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}

Code used to dismiss 用于关闭的代码

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        CGSize scaleSize = CGSizeMake(1024.0f, 1280.0f);
        UIImage *capturedImage;

        // Handle a still image capture
        capturedImage = (UIImage *) [info objectForKey:
                                     UIImagePickerControllerOriginalImage];

        [self dismissViewControllerAnimated:YES completion:nil];


        if (capturedImage) {
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
                photoData = UIImageJPEGRepresentation([capturedImage resizedImage:scaleSize interpolationQuality:kCGInterpolationHigh], 5);
                photoEntity = nil;

                [self.takePhotoButton setImage:[UIImage imageWithData:photoData ] forState:UIControlStateNormal];


                if ([self isAnyButtonSelected] || ![_answerTextField.text isEqualToString:@""] || !_questionBoolean.hidden) {
                    [Tools enableControl:_saveButton];
                }


            } else {
                newPhotoData = UIImageJPEGRepresentation([capturedImage resizedImage:scaleSize interpolationQuality:kCGInterpolationHigh], 5);
                photoEntity = nil;
            }
        }
    }

And here I tried messing with the parent views and controllers in this method. 在这里,我尝试用这种方法弄乱父视图和控制器。 I was able to get the app to return to the central view controller minus the current view controller that is in charge of taking the photos. 我能够使应用程序返回到中央视图控制器,减去负责拍照的当前视图控制器。 The only problem is that the app's touch is now disabled and I am missing one view. 唯一的问题是该应用程序的触摸现在已被禁用,而我缺少一个视图。

- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker
{
//   [self willMoveToParentViewController:nil];
//    [picker.view removeFromSuperview];
//   [picker removeFromParentViewController];
    [self dismissViewControllerAnimated:YES completion:nil];

}

I was going to post photos but I am currently unable to because I am new to stack overflow. 我本来要发布照片,但目前无法发布照片,因为我是堆栈溢出的新手。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

Ok so the app is not using story boards but it is using xib files. 好的,所以该应用程序未使用故事板,但正在使用xib文件。 It is inherited code and the app was created several years ago. 它是继承的代码,该应用程序是几年前创建的。 There are multiple view controllers with multiple views. 有具有多个视图的多个视图控制器。 There is a central view controller where all the other view controllers are added to the central view. 有一个中央视图控制器,其中所有其他视图控制器都添加到了中央视图中。

[self.view addSubview:_catViewController.tableView];
[self.view addSubview:_listViewController.tableView];
[self.view addSubview:_queryViewController.view];
[self.view bringSubviewToFront:_queryViewController.view];

queryViewController is where I am taking the photo. queryViewController是我拍照的地方。 When I dismiss every view is gone except the query view controller which happens to take up the entire screen (it previously did not). 当我关闭时,除查询视图控制器外,所有视图都消失了,而查询视图控制器恰好占据了整个屏幕(以前没有)。 Let me know if I need to add more information! 让我知道是否需要添加更多信息! Thanks 谢谢

So, the general problem is that your central view controller is adding the views of other view controllers to itself. 因此,一般的问题是您的中央视图控制器正在向其自身添加其他视图控制器的视图。 You can't do that -- it breaks view controller encapsulation. 您不能这样做-破坏了视图控制器的封装。 And when you violate encapsulation, stuff just breaks. 当您违反封装时,东西就会破裂。 (That's a vague statement, but 100% true :-p) Unfortunately, you either need to stab in the dark to find a hack to make it work, or repartition the problem to respect encapsulation. (这是一个模糊的陈述,但是100%是:-p)不幸的是,您要么需要在黑暗中刺入以找到使之起作用的hack,要么将其重新划分为尊重封装的问题。 The latter is probably better if you'll need to maintain this code in the future. 如果将来需要维护此代码,则后者可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 知道何时在iOS中关闭FBNativeDialogs - Knowing when FBNativeDialogs is dismissed in iOS Swift iOS-在ViewWIllAppear中,如何检查在切换选项卡或打开/关闭视图时是否显示或关闭了ImagePicker? - Swift iOS -In ViewWIllAppear How to Check If ImagePicker is Being Presented or Dismissed While Switching Tabs or The View Is Being Pushed On/Off? 使用UITabBarController(iOS SDK)切换选项卡时删除视图 - Removing views when switching tabs using UITabBarController ( iOS SDK ) 如何检测弹出窗口何时在 iOS 9 中被关闭 - How to detect when a popover is dismissed in iOS 9 SwiftUI:关闭 .contextMenu 时的通知 (iOS) - SwiftUI: Notification when .contextMenu is dismissed (iOS) 当用户不允许在iOS中访问时如何关闭imagePicker - How to dismiss imagePicker when user don't allow access in iOS 出现imagePicker时,iOS动画textView返回原始位置吗? - iOS animated textView returns to original position when imagePicker is presented? 在ios模拟器上运行时,当imagePicker的来源是Camera时应用程序崩溃? - While running on ios simulator, App crashes when source of imagePicker is Camera? 使用SplitViewController的iPad上的iOS Imagepicker - iOS Imagepicker on iPad with a SplitViewController 当我在iOS中输入空的AlertView文本字段时,不应关闭AlertViewController - AlertViewController should not be dismissed when I enter empty AlertView textfield in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM