我正在构建一个聊天应用程序,其中有两种类型的视图以模态方式呈现在聊天屏幕上:一个 UIAlertController 供用户选择要发送的媒体,以及一个弹出框,如果当前用户想要阻止/删除他们的用户重新发送消息。UIAlertController 运行良好,但阻止弹出窗口失败并显示以下警告:
“警告:尝试呈现不在窗口层次结构中的视图!”
这是演示流程:“收件箱”视图控制器呈现“聊天”视图控制器。这个“聊天”视图控制器是呈现模态 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)
}
块视图控制器:
@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)
}
我知道之前在 iOS (Swift) 上已针对此问题提出过类似的问题,但它们解释了与使用根视图控制器相关的解决方案。我不想使用根视图控制器。由于 UIAlertViewController 位于此屏幕上并且假定在视图层次结构中具有优先级,因此出现此问题。如何将拒绝用户VC 添加到层次结构或使其具有更高的优先级?先感谢您!