我该如何解决这个问题;找不到';目前';和';解雇';“范围内”;swift中的错误
我试图允许用户选择图像并将其设置为配置文件页面上的there横幅,但当我编写代码时,在尝试显示imagePicker时,我不断收到错误“在范围内找不到‘present’”,在尝试关闭imagePicker时,我也会收到错误“在范围内找不到‘disease’” 以下是我的上述代码:我该如何解决这个问题;找不到';目前';和';解雇';“范围内”;swift中的错误,swift,uiimagepickercontroller,Swift,Uiimagepickercontroller,我试图允许用户选择图像并将其设置为配置文件页面上的there横幅,但当我编写代码时,在尝试显示imagePicker时,我不断收到错误“在范围内找不到‘present’”,在尝试关闭imagePicker时,我也会收到错误“在范围内找不到‘disease’” 以下是我的上述代码: //this function gets called when the user tapes on the banner to change the image class ProfileHeader: UICol
//this function gets called when the user tapes on the banner to change the image
class ProfileHeader: UICollectionViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@objc func handleBannerTapped() {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let bannerPicture = info[UIImagePickerController.InfoKey.editedImage] as? UIImage else {
imageSelected = true
return
}
imageSelected = true
profileImage.layer.masksToBounds = true
profileBanner.setImage(bannerPicture.withRenderingMode(.alwaysOriginal), for: .normal)
dismiss(animated: true, completion: nil)
}
}
我在我的注册页面中使用了相同的功能,用户选择了一个配置文件图片,它工作正常,因此我不明白为什么它现在不适用于横幅您只能在视图控制器上显示和解除。但是ProfileHeader不是视图控制器。这是一间牢房 那么视图控制器在哪里?它在细胞的反应链上。因此,沿着响应者链一直走到视图控制器,并在该控制器上显示
present
和disclose
这里有一个实用方法可以帮助您:
extension UIResponder {
func next<T:UIResponder>(ofType: T.Type) -> T? {
let r = self.next
if let r = r as? T ?? r?.next(ofType: T.self) {
return r
} else {
return nil
}
}
}
等等
然而,尽管这会导致您的代码被编译,甚至看起来工作正常,但我认为您的方法完全是错误的:
- 首先,你不应该在牢房里做这项工作
- 您不应该让单元格告诉视图控制器要显示/取消显示的内容
- 您不应该随意修改单元格的内容
- 单元格是一个暂时的可重用对象,因此您甚至不能真正保证在选择图像时单元格仍在那里(或仍占据表的同一行)
相反,单元格应该只告诉视图控制器,嘿,用户想要选择一张照片,然后退后,让视图控制器完成工作。工作应该包括修改数据模型(然后重新加载单元格)。代码在哪里?我希望是在
UIViewController
中。只有UIViewController
可以调用present(\uu:animated:completion:)
您需要编辑问题以显示代码来自哪个类。包括类声明:例如,classmyvcclass:UIViewController{}
if let vc = self.next(ofType: UIViewController.self) {
vc.present(imagePicker, animated: true, completion: nil)
}