-2

Is it possible to add 3d touch to a table cell that gets the data of the new controller from Firebase?

I have this code below as an extension when I just press on the table cell it moves to the next controller fine and gets the correct data from Firebase, but when I use peek and pop I get a crash saying that the variable is not available. If I put static text in instead of getting it from the firebase array I get the same as well.

extension myVC: UIViewControllerPreviewingDelegate {

func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
    present(viewControllerToCommit, animated: true)
}


private func createDetailViewControllerIndexPath(indexPath: IndexPath) -> MainInfoVC {

    let dataPop = self.posts[indexPath.row]
    let infoPop = dataPop

    let storyboard = UIStoryboard(name: "ShowAndComments", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "MainInfoVC") as? MainInfoVC

    vc?.descriptionLabel.text = infoPop.desc

    return vc!
}

func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
    guard let indexPath = tableView.indexPathForRow(at: location) else { return nil }

    let  detailViewController = createDetailViewControllerIndexPath(indexPath: indexPath)
    return detailViewController
}
}

I also get a crash if I change this To..

vc?.descriptionLabel.text = "Some Text"

myApp was compiled with optimization - stepping may behave oddly; variables may not be available. (lldb)

enter image description here

Fattie
  • 27,874
  • 70
  • 431
  • 719
Tony Merritt
  • 1,177
  • 11
  • 35
  • 1
    Nothing to do with Firebase, as you yourself have said. So that shouldn't be in the title or the tags. The question thus devolves to: Can one do 3D touch for a table view cell in a master-detail architecture? Yes, one can, obviously; the Mail app is a case in point. (Probably the problem you're having has nothing to do with 3D touch either, though.) – matt Nov 29 '17 at 21:25
  • Thank you for your response on this. I had also come to the same conclusion and found that I didn't have to pass anything over as I had already obtained the info from firebase and saved it to each cell even though I wasn't displaying it. I was able to swap some code around and it now works. Again thank you for your time on this. – Tony Merritt Nov 29 '17 at 21:28
  • this just has no connection, at all, to firebase – Fattie Nov 29 '17 at 21:37
  • The data I was trying to get was coming from firebase and being stored into an array but I was only using 2 parts of the data in the cell that was displayed when I pushed the cell I wanted to display the rest of the data from firebase. the posts array is what is holding the firebase data. – Tony Merritt Nov 29 '17 at 21:42

2 Answers2

2

IBOutlets are nil early in a view controller's lifecycle. They are only non-nil after viewDidLoad. By default they are implicitly unwrapped, so if you message them before they have been initialized you'll get an unwrapped nil crash.

You should create a String property on MainInfoVC and set descriptionLabel's text property from viewDidLoad.

beyowulf
  • 15,101
  • 2
  • 34
  • 40
1

I was able to figure this out. Because i had already downloaded the data from firebase and was holding it in an array, I was able to change the code around and get it to work.

extension myVC: UIViewControllerPreviewingDelegate {

func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
     let navigationController = UINavigationController(rootViewController: viewControllerToCommit)
}

func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
    guard let indexPath = tableView.indexPathForRow(at: location) else { return nil }

    let storyboard = UIStoryboard(name: "ShowAndComments", bundle: nil)

    let vc = storyboard.instantiateViewController(withIdentifier: "MainInfoVC") as? MainInfoVC

    let dataPop = self.posts[indexPath.row]
    let platePop = dataPop.postKey

    return vc
}
}
Tony Merritt
  • 1,177
  • 11
  • 35