1

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)
}
uuuuuu
  • 119
  • 1
  • 7
  • What do you mean by "objC"-function? A function written in Objective C (not seen here), or that "didLongPress" swift function which is marked with @objc (indicating that it participates in the Objective C runtime)? – Andreas Oetjen Mar 15 '21 at 20:29
  • @ Andreas Oetjen Yes I mean that "didLongPress" swift function. It had to be marked as objC to work in the override let long press function. – uuuuuu Mar 15 '21 at 22:26
  • The problem seems that you want to return a string from a void function. The question is: Even if you change the return type, the gesture recognizer won't deal with it. I assume you want to show some sort of "ImagePreviewController" upon long press, do you? – Andreas Oetjen Mar 15 '21 at 22:37
  • I assume you want to show some sort of "ImagePreviewController" upon long press, do you? - yes that is exactly right – uuuuuu Mar 16 '21 at 07:46
  • You should not extend your questions; if a different aspect occurs, you should first search, and if you did not find anything, you should ask another question. All those dimension-related stuff does not match to the question subject any more; nobody who searches for long-press-stuff is expecting frame sizing things. – Andreas Oetjen Mar 16 '21 at 16:06
  • Hmm I kind of disagree. The question was how you call it from ObjC func instead of ContextMenuInteraction. In ContextMenuInteraction it uses func ImagePreviewController and gets the sizing right. In the move to ObjC func the sizing went haywire. As such, the movement from ContextMenuInteraction to ObjC func is not yet properly complete. Regardless you answer was on point so I will mark it as correct – uuuuuu Mar 16 '21 at 20:38

1 Answers1

0

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")
    }
}
Andreas Oetjen
  • 9,889
  • 1
  • 24
  • 34
  • Thanks. I will add to the question (probably should have) that this class is `class ViewControllerTableViewCell: UITableViewCell, UIContextMenuInteractionDelegate` I am getting error at present `Value of type 'ViewControllerTableViewCell' has no member 'present'` Would this only work in a class ViewController? The ImagePrieviewController class is UIViewController – uuuuuu Mar 16 '21 at 13:00
  • I did some searching and it indeed looks like that is the problem. Perhaps I could add a protocol to communicate with UIViewController – uuuuuu Mar 16 '21 at 14:19
  • So I think I can make it work with your answer and three little modifications I added in the question. Only issue that then remains is getting the preview in the correct dimensions. For some reason the preview is almost full screen at the moment. I added to the question the dimension I used successful in the past with contextMenuInteraction – uuuuuu Mar 16 '21 at 14:56