'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 1:[1]
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)
}
}
}
Solution 2:[2]
You need to communicate back to the ViewController that presents this TableView. Either with a remote notification, or delegate.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | COSMO BAKER |
