iosswiftobjective-clong-pressuilongpressgesturerecogni

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


This function below needs to instead go into an ObjC function

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

Now Obj C function

    @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 gets handle in override function

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

What error am I currently getting:

Non void in void function. That is if I take the ringy/image3 and green/image3 part of the code and put it is the objC function.

Update after first answer

I seem to get it working with the answer and some modifications

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

Only issue I have left is that the dimension of the ImagePreviewController are wrong/virtually full screen. They should be:

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

Solution

  • If you want to present a new view controller, you could either perform a segue (if you work with storyboards) or do it the classic way:

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