【发布时间】:2017-08-10 00:30:13
【问题描述】:
我在从 collectionViewCell 呈现 UIImagePicker 时遇到了一点问题。 I know collectionViewCells do not act as the picker delegate by default so I did try to call a method from the UIViewController class operating the cell holding the imageView tapped.我的控制台中没有显示任何错误,但是当我运行应用程序并尝试呈现 UIImagePickerController 时没有任何反应,并且我收到“警告:尝试呈现不在窗口层次结构中的视图!”。当用户点击 profileImageView 时,我想展示 UIImagePickerController。提前感谢您的帮助!
// 持有imageView的LoginCell类调用LoginController类中的UIImagePickerController方法
lazy var profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(handleSelectProfileImage)))
imageView.isUserInteractionEnabled = true
return imageView
}()
var loginController: LoginController?
func handleSelectProfileImage() {
let loginController = LoginController()
loginController.showImagePicker(sendingVC: loginController)
}
// 作为 UIImagePickerController 委托的 LoginController 类
func showImagePicker(sendingVC: LoginController) {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
present(picker, animated: true, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let loginCell = collectionView.dequeueReusableCell(withReuseIdentifier: loginCellId, for: indexPath) as! LoginCell
loginCell.delegate = self
return loginCell
}
【问题讨论】:
-
你设置 loginController 了吗?
-
我做了,也看到更新的代码。我现在收到有关窗口层次结构的警告消息。
-
如果你的 UICollectionView 在你的 LoginViewController 中,你应该在 cellForItemAtIndexPath 中设置对 LoginViewController 的引用,而不是创建一个新的 LoginController 实例。
-
好的,有道理,谢谢。所以我的 loginCell 一直设置为 cellForItemAtIndexPath 中的委托。当我尝试将 LoginCell 中的 UIImagePickerController 显示为委托时,我的应用程序仍然崩溃。请参阅原始帖子中的更新代码。
标签: swift delegates uiimagepickercontroller uicollectionviewcell