2017-06-15 63 views
0

將數據從一個ViewController傳遞到另一個ViewController後出現以下錯誤。在兩個ViewController之間傳遞數據時出錯

no data 
eventStruct(title: What are we gonna do today?, date: 2017-07-13, location: House3, description: Hello beautiful people!) 
2017-06-15 22:01:05.697 RUC-App 1.0[48760:5792168] Warning: Attempt to present <RUC_App_1_0.EventViewController: 0x7fb686636460> on <RUC_App_1_0.NewsfeedViewController: 0x7fb68662e6a0> whose view is not in the window hierarchy! 

從第一ViewController代碼:

import UIKit 
import Firebase 
import FirebaseAuth 
import FirebaseDatabase 

class NewsfeedViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    var ref:DatabaseReference!, 
     posts = [eventStruct]() 
    @IBOutlet weak var tableview: UITableView! 
    var propertStruct : (Any)? = nil 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     loadNews() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    func loadNews() { 
     ref = Database.database().reference() 
     ref.child("events").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in 

      if let valueDictionary = snapshot.value as? [AnyHashable:String] 
      { 
       let title = valueDictionary["Title"] 
       let location = valueDictionary["Location"] 
       let date = valueDictionary["Date"] 
       let description = valueDictionary["Description"] 
       self.posts.insert(eventStruct(title: title, date: date, location: location, description: description), at: 0) 
       self.tableview.reloadData() 
      } 
     }) 
    } 

    /////////////////////////  Table View Content  \\\\\\\\\\\\\\\\\\\\\\\\\\ 
    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 
     let label1 = cell.viewWithTag(1) as! UILabel 
     label1.text = posts[indexPath.row].title 
     let label2 = cell.viewWithTag(2) as! UILabel 
     label2.text = posts[indexPath.row].location 
     let label3 = cell.viewWithTag(3) as! UILabel 
     label3.text = posts[indexPath.row].date 
     return cell 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if (segue.identifier == "showDetails"){ 
      let secondViewController = segue.destination as? EventViewController 
      secondViewController?.data = sender as? eventStruct 
     } 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     performSegue(withIdentifier: "showDetails", sender: posts[indexPath.row]) 
    } 
    /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 
} 

struct eventStruct { 
    let title: String! 
    let date: String! 
    let location: String! 
    let description: String! 

} 

第二ViewController的代碼:

import UIKit 

class EventViewController: UIViewController { 

    var data: eventStruct? 
    @IBOutlet weak var titleLabel: UILabel! 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     print (data ?? "no data") 
     self.titleLabel.text = self.data?.title 

     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 
+0

你有沒有連接'showDetails'原因請看您tableviewcell *和*你直接在'didSelectRowAt'中調用它? – DonMag

+0

是的。我就是這麼做的。 –

回答

2

你不應該有觸發和手動塞格斯同時發生時間。

試試這個:

刪除您didSelectRowAt功能(或將其註釋掉),並將其替換prepare()

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if (segue.identifier == "showDetails"){ 
     let secondViewController = segue.destination as? EventViewController 
     if let idx = tableView.indexPathForSelectedRow { 
      secondViewController?.data = posts[idx.row] 
     } 
    } 
} 
+0

這解決了這個問題。謝謝! –

相關問題