2017-06-22 1 views
1

Je veux prendre le titre et le mettre comme titleText dans le DestVC qui est une étiquette. Comment est-ce que je le mets dans la fonction de segue?Comment mettre du texte dans une fonction de segue pour passer au contrôleur suivant?

import UIKit 
import Firebase 
import FirebaseDatabase 
import SDWebImage 


struct postStruct { 
    let title : String! 
    let author : String! 
    let date : String! 
    let article : String! 
    let downloadURL : String! 

} 

class ZeroHomeViewController: UITableViewController { 




var posts = [postStruct]() 
var downloadURL : String = "" 






override func viewDidLoad() { 
    super.viewDidLoad() 


    let ref = Database.database().reference().child("Posts") 

    ref.observeSingleEvent(of: .value, with: { snapshot in 

     print(snapshot.childrenCount) 

     for rest in snapshot.children.allObjects as! [DataSnapshot] { 

      guard let value = rest.value as? Dictionary<String,Any> else { continue } 

      guard let title = value["Title"] as? String else { continue } 
      guard let downloadURL = value["Download URL"] as? String else { continue } 
      guard let author = value["Author"] as? String else { continue } 
      guard let date = value["Date"] as? String else { continue } 
      guard let article = value["Article"] as? String else { continue } 


      let post = postStruct(title: title, author: author, date: date, article: article, downloadURL: downloadURL) 

      self.posts.append(post) 


     } 

     self.posts = self.posts.reversed(); self.tableView.reloadData() 

    }) 


} 




override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return posts.count 
} 

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

    let label1 = cell?.viewWithTag(1) as! UILabel 
    label1.text = posts[indexPath.row].title 


    let imageView = cell?.viewWithTag(2) as! UIImageView 
    let post = self.posts[indexPath.row]; 
    imageView.sd_setImage(with: URL(string: post.downloadURL), placeholderImage: UIImage(named: "placeholder")) 

return cell! 

} 


override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "detail" { 
     if let indexPath = tableView.indexPathForSelectedRow { 
      let destVC = segue.destination as! ArticleViewController 
      destVC.titleText = value["Title"] as? String 

     } 

    } 
} 

}

+0

'destVC.titleText = valeur [" Titre "] as? Chaîne'? – Paulw11

+0

ça ne marche pas. dit une référence ambiguë à la valeur du membre (forKey :) ' – Riccardo

+0

Qu'est-ce que 'value'? Vous n'avez pas montré où/quoi que ce soit dans votre code – Paulw11

Répondre

2

Il vous suffit d'accéder au postStruct pertinent de votre tableau posts puis obtenir le titre. Vous avez déjà le chemin d'index pour la ligne sélectionnée; la propriété .row sera l'index de votre tableau posts pour la structure dont vous avez besoin.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "detail" { 
     if let indexPath = tableView.indexPathForSelectedRow { 
      let destVC = segue.destination as! ArticleViewController 
      destVC.titleText = posts[indexPath.row].title 

     } 

    } 
} 
+0

travaillé! Merci! – Riccardo