Ios 在单元格中点击imageview时如何转到其他视图

Ios 在单元格中点击imageview时如何转到其他视图,ios,swift4,xcode9,Ios,Swift4,Xcode9,我有一个使用自定义单元格(xib)的表视图 此自定义单元格具有图像和说明。 如果用户点击图像(而不是单元格),则应显示其他页面/视图 这是在点击图像时调用的函数 @objc func imageTapped(){ let sb = UIStoryboard(name: "SbIdentifier", bundle: nil) let vc = sb.instantiateViewController(withIdentifier: "ProfileViewController")

我有一个使用自定义单元格(xib)的表视图

此自定义单元格具有图像和说明。 如果用户点击图像(而不是单元格),则应显示其他页面/视图

这是在点击图像时调用的函数

@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)
 }

我尝试使用推送视图控制器,但没有任何结果。

RootViewController没有父级,这肯定没有navigationController。如果navigationController是层次结构中的第一个viewController,则可以尝试:

(self.window?.rootViewController as? UINavigationController)?.pushViewController(vc, animated: true)

RootViewController不会有父级,而父级肯定不会有navigationController。如果navigationController是层次结构中的第一个viewController,则可以尝试:

(self.window?.rootViewController as? UINavigationController)?.pushViewController(vc, animated: true)
您可以使用此处处理在tableViewCell中点击配置文件图像以推送ProfileViewController

首先创建
CellImageTappableDelegate
协议

protocol CellImageTappableDelegate: class {
    func didTapProfileImage(with imageUrl: String)
}
然后在CustomCell中创建委托属性

weak var imageTapDelegate: CellImageTappableDelegate?
现在在
UIImageView

image.isUserInteractionEnabled = true
然后将
tappesturerecognizer
添加到imageView并实现选择器

let tapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(CustomProfileCell.didTapProfileImage(_:)))
        profileImageView.addGestureRecognizer(tapGestureRecognizer)

func didTapProfileImage(_ tapGesture: UITapGestureRecognizer) {
     self.imageTapDelegate?.didTapProfileImage(with: self.profileImageUrl)
}
在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)
}
您可以使用此处处理在tableViewCell中点击配置文件图像以推送ProfileViewController

首先创建
CellImageTappableDelegate
协议

protocol CellImageTappableDelegate: class {
    func didTapProfileImage(with imageUrl: String)
}
然后在CustomCell中创建委托属性

weak var imageTapDelegate: CellImageTappableDelegate?
现在在
UIImageView

image.isUserInteractionEnabled = true
然后将
tappesturerecognizer
添加到imageView并实现选择器

let tapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(CustomProfileCell.didTapProfileImage(_:)))
        profileImageView.addGestureRecognizer(tapGestureRecognizer)

func didTapProfileImage(_ tapGesture: UITapGestureRecognizer) {
     self.imageTapDelegate?.didTapProfileImage(with: self.profileImageUrl)
}
在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)
}


你能看到打印报表日志吗?是的,Prashant。我可以看到打印语句,而不是处理
self.window?.rootViewController
您可以委托给具有TableView的viewcontroller。因此,它可以直接推送您的viewcontroller。它是安全的,不会影响当前视图层次结构。您是否可以看到打印语句日志?是的,Prashant。我可以看到打印语句,而不是处理
self.window?.rootViewController
您可以委托给具有TableView的viewcontroller。因此,它可以直接推送您的viewcontroller。它是安全的,不会影响当前视图层次结构。我也尝试了您的代码和这行代码:self.window?.rootViewController?.navigationController?.pushViewController(vc,动画:true),但仍然没有working@ChristianLeppong很好,你试过了。。它起作用了吗?如果没有,那么您的视图控制器层次结构比您想象的更复杂,您将不得不共享更多的代码,以便我们能够帮助YouTunks Milan。我认为我需要访问父视图控制器,因为我需要从UITableViewCell推送视图控制器。@ChristianReplpong窗口的rootViewController是视图控制器,它是视图控制器层次结构中的第一个视图控制器。。假设是导航控制器,其中嵌入了其他一些视图控制器(我猜是您的tableViewController)。。通常只有一个窗口,上面有许多视图控制器。。您正在访问窗口并从根开始运行。@christianreppong我认为最好的方法是设置一个委托模式,告诉tableViewController显示/推送视图控制器,但是如果您的层次结构很简单,并且您知道它不会改变,那么这就足够了。。将
cellForRowAt
实现添加到您的qstionive中,您也尝试了您的代码,这行代码是:self.window?.rootViewController?.navigationController?.pushViewController(vc,动画:true),但仍然没有working@ChristianLeppong很好,你试过了。。它起作用了吗?如果没有,那么您的视图控制器层次结构比您想象的更复杂,您将不得不共享更多的代码,以便我们能够帮助YouTunks Milan。我认为我需要访问父视图控制器,因为我需要从UITableViewCell推送视图控制器。@ChristianReplpong窗口的rootViewController是视图控制器,它是视图控制器层次结构中的第一个视图控制器。。假设是导航控制器,其中嵌入了其他一些视图控制器(我猜是您的tableViewController)。。通常只有一个窗口,上面有许多视图控制器。。您正在访问窗口并从根开始运行。@christianreppong我认为最好的方法是设置一个委托模式,告诉tableViewController显示/推送视图控制器,但是如果您的层次结构很简单,并且您知道它不会改变,那么这就足够了。。将
cellForRowAt
实现添加到您的qustiOnAnks中,这会让您感到非常高兴。我错过了“cell.imageTapDelegate”很高兴它有帮助。非常感谢suhit。我错过了手机,很高兴它能帮上忙。