[英]How to go to other view when tapped imageview in a cell
I have a table view using a custom cell (xib). 我有一个使用自定义单元格(xib)的表格视图。
This custom cell have an image and a description. 该自定义单元格具有图像和描述。 If user tapped the image (not the cell) , it should display other page/view. 如果用户点击图像(而不是单元格),它应该显示其他页面/视图。
this is the function called when image tapped 点击图像时调用的函数
@objc func imageTapped(){
let sb = UIStoryboard(name: "SbIdentifier", bundle: nil)
let vc = sb.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController
print("image tapped")
//self.window?.rootViewController?.parent?.navigationController?.pushViewController(vc, animated: true)
}
I tried used the push view controller but nothing happened. 我尝试使用推式视图控制器,但没有任何反应。
You can use delegate pattern here to handle tap on profile image in tableViewCell to push ProfileViewController. 您可以在此处使用委托模式来处理对tableViewCell中的个人资料图像的点击,以推送ProfileViewController。
First create the CellImageTappableDelegate
protocol 首先创建CellImageTappableDelegate
协议
protocol CellImageTappableDelegate: class {
func didTapProfileImage(with imageUrl: String)
}
then create the delegate property in Your CustomCell 然后在您的CustomCell中创建委托属性
weak var imageTapDelegate: CellImageTappableDelegate?
now enable isUserInteractionEnabled
property on UIImageView
现在在UIImageView
上启用isUserInteractionEnabled
属性
image.isUserInteractionEnabled = true
then add the tapGestureRecognizer
to your imageView and implement the selector 然后将tapGestureRecognizer
添加到您的tapGestureRecognizer
并实现选择器
let tapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(CustomProfileCell.didTapProfileImage(_:)))
profileImageView.addGestureRecognizer(tapGestureRecognizer)
func didTapProfileImage(_ tapGesture: UITapGestureRecognizer) {
self.imageTapDelegate?.didTapProfileImage(with: self.profileImageUrl)
}
in tableView datasource cellForRowAt:
method in your UIViewController
subclass assign the delegate to self
and implement the protocol method 在tableView数据源cellForRowAt:
中,您的UIViewController
子类中的方法将委托分配给self
并实现协议方法
cell.imageTapDelegate = self
func didTapProfileImage(with imageUrl: String) {
print("Profile image tapped")
let storyboard = UIStoryboard(name: "SbIdentifier", bundle: nil)
let profileVC = storyboard.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController
profileVC.imageUrl = imageUrl
self.navigationController?.pushViewController(profileVC, animated: true)
}
RootViewController would not have a parent, which would definitely not have a navigationController. RootViewController没有父级,后者肯定没有NavigationController。 If the navigationController is the first viewController in your hierarchy, you can try: 如果navigationController是层次结构中的第一个viewController,则可以尝试:
(self.window?.rootViewController as? UINavigationController)?.pushViewController(vc, animated: true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.