简体   繁体   English

警告:尝试在视图不在窗口层次结构中的ViewController上显示ViewController(带有UIAlertController)

[英]Warning: Attempt to present ViewController on ViewController whose view is not in the window hierarchy (w/ UIAlertController)

I'm building a chat app where there are two types of views to be modally presented over the chat screen: a UIAlertController for users to select media to send, and a popover if the current user wants to block/remove the user that they're messaging. 我正在构建一个聊天应用程序,其中在聊天屏幕上可以模态呈现两种视图:一种用于用户选择要发送的媒体的UIAlertController;如果当前用户想要阻止/删除该用户,则显示一个弹出窗口重新发送消息。 The UIAlertController comes up fine, but the blocking popover fails with this warning: UIAlertController正常运行,但是阻塞弹出窗口失败并显示以下警告:

"Warning: Attempt to present on whose view is not in the window hierarchy!" “警告:尝试呈现不在窗口层次结构中的视图!”

Here is the presentation flow: an "inbox" view controller presents a "chat" view controller. 这是演示流程:“收件箱”视图控制器提供“聊天”视图控制器。 This "chat" view controller is the one that presents the modal UIAlertController and the "block" view controller (also modal, over current context). 此“聊天”视图控制器是一种呈现模式UIAlertController和“块”视图控制器(在当前上下文中也是模式)的控制器。 Here is my code where I deal with both of these modal views: 这是我处理这两种模式视图的代码:

UIAlertController: UIAlertController:

    @IBAction func mediaBtnWasPressed(_ sender: Any) {
    let alert = UIAlertController(title: "Glymps", message: "Please select a source:", preferredStyle: UIAlertController.Style.actionSheet)
    let camera = UIAlertAction(title: "Take a picture", style: UIAlertAction.Style.default) { (_) in

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
            self.picker.sourceType = .camera
            self.present(self.picker, animated: true, completion: nil)
        } else {
            print("Option unavailable.")
        }
    }
    let video = UIAlertAction(title: "Take a video", style: UIAlertAction.Style.default) { (_) in

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
            self.picker.sourceType = .camera
            self.picker.mediaTypes = [String(kUTTypeMovie)]
            self.picker.videoExportPreset = AVAssetExportPresetPassthrough
            self.picker.videoMaximumDuration = 30
            self.present(self.picker, animated: true, completion: nil)
        } else {
            print("Option unavailable.")
        }
    }
    let library = UIAlertAction(title: "Choose an image or video", style: UIAlertAction.Style.default) { (_) in

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary) {
            self.picker.sourceType = .photoLibrary
            self.picker.mediaTypes = [String(kUTTypeImage), String(kUTTypeMovie)]
            self.present(self.picker, animated: true, completion: nil)
        } else {
            print("Option unavailable.")
        }
    }
    let cancel = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil)

    alert.addAction(camera)
    alert.addAction(video)
    alert.addAction(library)
    alert.addAction(cancel)

    self.present(alert, animated: true, completion: nil)
   }

Block View Controller: 块视图控制器:

@IBAction func declineBtnWasPressed(_ sender: Any) {
    // remove user from current user message requests and matches
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let declineUserVC = storyboard.instantiateViewController(withIdentifier: "DeclineUserVC") as! DeclineUserVC
    declineUserVC.userId = self.userId
    self.present(declineUserVC, animated: true, completion: nil)
}

I understand similar questions have been asked before for this issue on iOS (Swift), but they explain solutions relative to using a root view controller. 我知道以前曾在iOS(Swift)上针对此问题提出过类似的问题,但它们解释了有关使用根视图控制器的解决方案。 I am not trying to use a root view controller. 我不尝试使用根视图控制器。 This problem is occurring since the UIAlertViewController is on this screen and supposedly taking priority in the view hierarchy. 因为UIAlertViewController在此屏幕上并且据认为在视图层次结构中具有优先级,所以会出现此问题。 How do I add the declineUserVC to the hierarchy or have it take higher priority? 如何将拒绝用户VC添加到层次结构中或使其具有更高的优先级? Thank you in advance! 先感谢您!

Screenshot 屏幕截图

I've figured out the issue. 我已经解决了这个问题。 As you can see in the picture below, there is an action for the declineUser button that somehow got inside the action for the back button: 正如您在下图中所看到的那样,存在一个拒绝用户按钮的操作,该按钮以某种方式进入了后退按钮的操作:

Bad IBAction 错误的IBAction

Once I delete that so there is only one IBAction for the declineUser button, it works. 删除该行后,只有一个IBAction可以用于拒绝用户按钮,它可以正常工作。 The app was getting confused to which view the button was on and which was triggering the action, thus throwing a view hierarchy error. 该应用程序对按钮处于哪个视图上以及哪个按钮正在触发操作感到困惑,从而引发了视图层次结构错误。 Lesson learned: always check how your IBOutlets and IBActions are set up! 经验教训:始终检查如何设置IBOutlets和IBAction!

暂无
暂无

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

相关问题 警告:尝试在视图不在窗口层次结构中的ViewController上显示ViewController - Warning: Attempt to present ViewController on ViewController whose view is not in the window hierarchy NSNotification:尝试在ViewController上显示UIAlertController,其视图不在窗口层次结构中 - NSNotification: Attempt to present UIAlertController on ViewController whose view is not in the window hierarchy 尝试呈现<UIAlertController: >在<ViewController:>其视图不在窗口层次结构中 - Attempt to present <UIAlertController: > on <ViewController:> whose view is not in the window hierarchy 警告:尝试呈现其视图不在窗口层次结构中的ViewController - Warning: attempt to present ViewController whose view is not in the window hierarchy 警告:尝试在视图不在窗口层次结构中的 **.ViewController 上显示 ZMNavigationController - Warning: Attempt to present ZMNavigationController on **.ViewController whose view is not in the window hierarchy 警告:尝试在其视图不在窗口层次结构中的 TabBarController 上显示 ViewController - Warning: Attempt to present ViewController on TabBarController whose view is not in the window hierarchy iOS警告:尝试呈现视图不在窗口层次结构中的ViewController - iOS Warning: attempt to present ViewController whose view is not in the window hierarchy 警告:尝试在视图不在窗口层次结构中的ViewController上显示UINavigationController - Warning: Attempt to present UINavigationController on ViewController whose view is not in the window hierarchy 警告“尝试呈现视图不在窗口层次结构中的ViewController-目标C - Warning “attempt to present ViewController whose view is not in the window hierarchy - Objective C iOS:警告“尝试呈现其视图不在窗口层次结构中的ViewController” - iOS: Warning “attempt to present ViewController whose view is not in the window hierarchy”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM