iosswiftxcodeuitableviewpresentviewcontroller

Unable to present in a UITableViewCell


The following code works in a UIViewController, but in my class thats a UITableViewCell it's giving the error

Use of unresolved identifier 'present'.

The code is an action:

@IBAction func linkAction(_ sender: Any) { 
  let linkString = self.linkText.text 
  if let requestUrl = URL(string: linkString!) {
     let safariVC = SFSafariViewController(url: requestUrl)
     present(safariVC, animated: true)  
  }
}

Is there a fix?


Solution

  • In the main UIViewController:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
      let cell = tableView.dequeueReusableCell(withIdentifier: "idCell", for: indexPath)
      cell.viewController = self
     
    }
    

    In the UITableViewCell class:

    class TableViewCell: UITableViewCell {
      
      weak var viewController: UIViewController?
    
      @IBAction func linkAction(_ sender: Any) { 
        let linkString = self.linkText.text 
        if let requestUrl = URL(string: linkString!) {
           let safariVC = SFSafariViewController(url: requestUrl)
           viewController?.present(safariVC, animated: true, completion: nil)
        } 
      }
    }