如何正确地将信息从TableView转换为ViewController

时间:2016-10-12 17:39:49

标签: ios swift uitableview segue

我尝试通过segView从TableView传递一些信息到ViewController。

通过我的调试,我能够看到ViewController在TableView中设置之前读取变量。这导致值为nil(它是可选的)而不是我期望的值,但是如果我回到我的tableView并再次选择该值,它将自己设置正确。

我是否需要以某种方式减慢viewDidLoad()中的读取速度,或者我是否遗漏了来自tableView的segueing的一些基本部分?我很好奇为什么这个值第一次为零,但是第二次有效。

TableView中的segue:

// MARK: - Send information to the FBOSelector
// Send the fieldName the user selects
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

let destination = segue.destinationViewController as! FBOSelectorViewController

let path = tableView.indexPathForSelectedRow
destination.fieldName = self.locations[(path?.row)!]

self.fetchFbos(self.locations[(path?.row)!])

}

在TableView中设置我将在viewController中拉入的值:

// This is failing because the values are read in FBOSelector before they are set here
// Using the fieldname selected pass the rest of the information to FBOSelector
func fetchFbos(fieldName: String) {
    let ref = FIRDatabase.database().reference().child("Airport/\(fieldName)/FBOs/Signature")

    ref.observeEventType(.Value, withBlock: { (snapshot) in
        //           print(snapshot)
        if let dictionary = snapshot.value as? [String: AnyObject] {
            RegistrationsManager.sharedManager.activeReservation.firfullName = dictionary["fullname"] as? String
            // This will fire after the value in FBOSelector is read
            print("DELAYED SETTER")
            print(RegistrationsManager.sharedManager.activeReservation.firfullName)
        }
    })
}

在TableView中我设置了一些带有我在TableView中设置的值的标签,但是它们返回nil(第一次,如果我回去再次选择它们就会正确显示)

func firebaseInfo() {
    self.fieldNameLabel.text = fieldName
    self.locationLabel.text = RegistrationsManager.sharedManager.activeReservation.firfullName
    //*** BUG IS HERE ***
    // On first click locationLabel is nil
//        print("DEBUG INFO")
        print(RegistrationsManager.sharedManager.activeReservation.firfullName)
}

然后我在viewDidLoad中运行它:

override func viewDidLoad() {
    super.viewDidLoad()

    fboCollectionView.delegate = self
    fboCollectionView.dataSource = self
    fboCollectionView.pagingEnabled = true

    firebaseInfo()
}

对我来说似乎是一个速度问题/并发问题?我尝试使用viewWillAppear(相同的问题)和使用sleep()来获取ViewController中的值(不好,因为它会停止所有处理)。有人能指出我正确的解决方案吗?

2 个答案:

答案 0 :(得分:0)

这是我实现传递数据的方法。

TableViewController

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let selectedIndexPath:NSIndexPath = self.tableView.indexPathForSelectedRow!
        let genVC:genViewController = segue.destinationViewController as! genViewController
        genVC.selectedItem = myData[selectedIndexPath.row]

}

GenViewController

var selectedItem: NSManagedObject?
override func viewDidLoad() {
   super.viewDidLoad()
   fetchData()
}

fetchData()将是根据发送的索引从数据库中提取数据的代码。

答案 1 :(得分:0)

嘿所以我会尝试在didSelectCellAtIndexPath中调用fetchFbos(indexPath: indexPath)并从self.fetchFbos(_:)删除prepareForSegue

以下fetchFbos(indexPath: IndexPath)将在从firebase获取新值后执行segue。

func fetchFbos(indexPath: IndexPath) {
 let fieldName = self.locations[indexPath.row]
 let ref = FIRDatabase.database().reference().child("Airport/\(fieldName)/FBOs/Signature")

ref.observeEventType(.Value, withBlock: { (snapshot) in
    //           print(snapshot)
    if let dictionary = snapshot.value as? [String: AnyObject] {
        RegistrationsManager.sharedManager.activeReservation.firfullName = dictionary["fullname"] as? String
        // This will fire after the value in FBOSelector is read
        print("DELAYED SETTER")
        self.performSegue(identifier: segueId, 
       sender: self)
        print(RegistrationsManager.sharedManager.activeReservation.firfullName)
    }
})
}