长按时,如何从 objective c 函数而不是 contextMenuInteraction 函数调用 ImagePreviewController: UIViewController?

On long press, how do you call an ImagePreviewController: UIViewController from an objective c function instead of a contextMenuInteraction function?

下面的这个函数需要改为进入 ObjC 函数

///this 1st func is in UITableViewController, the others are in UITableViewCell

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

          if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? ViewControllerTableViewCell {

 ...}

 class ViewControllerTableViewCell: UITableViewCell, UIContextMenuInteractionDelegate

func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
    UIContextMenuConfiguration(identifier: nil, previewProvider: {
   if self.cc == interaction  {
            let image3 = UIImage(named:"ringy.png")
            if let unwrappedImage1 = image3 {
                return ImagePreviewController(image:unwrappedImage1) 
            }
            else {
                return nil
            }

        }
        else if self.vv == interaction{
            let image3 = UIImage(named:"green.png")
            if let unwrappedImage1 = image3 {
                return ImagePreviewController(image:unwrappedImage1)   
            }
            else {
                return nil
            }
        }

        else {
                      return nil
                  }
            })
            }

现在是 Obj C 函数

    @objc func didLongPress() {
  ///database call
            if ab as! Int>0 {
 /// here the part for ringy.png needs to go
            } else {
  /////here the part for green.png needs to go
            }
        }
        else {
            print("No data available")
        }
    })
}

ObjC 在覆盖函数中获取句柄

   let longPress = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress))
    like?.addGestureRecognizer(longPress)

我目前遇到了什么错误:

无效函数中的非无效。也就是说,如果我将代码的 ringy/image3 和 green/image3 部分放入 objC 函数中。

第一个回答后更新

我似乎可以使用答案和一些修改

  1. weak var viewController: UIViewController?
    
  2. viewController?.present(previewVC, animated: true, completion: nil)
    
  3. cell.viewController = self ///inside cellForRowAt
    

我唯一剩下的问题是 ImagePreviewController 的尺寸是 wrong/virtually 全屏。他们应该是:

  class ImagePreviewController: UIViewController {
private let imageView = UIImageView()
init(image: UIImage) {
    super.init(nibName: nil, bundle: nil)
    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true
    imageView.image = image
    view = imageView
    let size = UIScreen.main.bounds.size
    preferredContentSize = .init(width: size.width, height: size.height/1.55)
}
required init?(coder: NSCoder) {
    super.init(coder: coder)
}

如果你想展示一个新的视图控制器,你可以执行一个 segue(如果你使用故事板)或者以经典方式进行:

@objc func didLongPress() {
    var image:UIImage?
    // Some database call? How to get "ab"?
    // Maybe you should not use as! in the following check:
    if ab as! Int>0 {
        image = UIImage(named:"ringy.png")
    } else {
        image = UIImage(named:"green.png")
    }
        
    if let image = image {
        let previewVC = ImagePreviewController(image:image)
        self.present(previewVC, animated: true, completion: nil)
    } else {
        print("No data available")
    }
}