2017-08-04 48 views
0

我一直在寻找这个答案的天。每当我尝试实例化一个名为“Govind”的新视图控制器时,我都会遇到SIGARBT错误。我得到SIGARBT错误的原因是因为在调用的视图控制器中我使用了变量; yourVariable,ASIN,VariationImages,在我的数据库中查找特定节点。当我将它们设置为等于firebase的值时,yourVariable,ASIN和VariationImages的值不会更改。来自Firebase的值不是零。这里是我的代码斯威夫特全球变数不更新

import UIKit 
    import Firebase 
    import FirebaseDatabase 
    var yourVariable = "" 
    var ProductsNumber = 100 
    var ASIN = "" 
    var Weblink = "" 
    var VariationImages = 5 

    class Initial: UIViewController, UICollectionViewDataSource, 
    UICollectionViewDelegate { 
    @IBOutlet weak var FrontPageCollectionView: UICollectionView! 

    var UIFrame = UIScreen.main.bounds 
    var ref: DatabaseReference! 
    var DatabaseHandle = nil as DatabaseHandle! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     ref = Database.database().reference() 

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

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 3 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     self.DatabaseHandle = ref.child("Frontpage").child(String(indexPath.row)).observe(.value, with: { (TheCategory) in 
      yourVariable = TheCategory.childSnapshot(forPath: "Category").value as! String 
      ASIN = TheCategory.childSnapshot(forPath: "ASIN").value as! String 
     self.DatabaseHandle = self.ref.child(TheCategory.childSnapshot(forPath: "Category").value as! String).child(TheCategory.childSnapshot(forPath: "ASIN").value as! String).child("VariationImages").observe(.value, with: { (NumberOfVariationImages) in 
      VariationImages = Int(NumberOfVariationImages.childrenCount) 
      }) 
     }) 
     CallGovind() 
    } 

    func CallGovind() { 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let controller = storyboard.instantiateViewController(withIdentifier: "Govind") 
     controller.modalPresentationStyle = .popover 
     self.present(controller, animated: true, completion: nil) 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FrontpageCell", for: indexPath) as! myCell 
     self.DatabaseHandle = ref.child("Frontpage").child(String(indexPath.row)).child("Thumbnail").observe(.value, with: { (snapshot) in 
      cell.FrontpageImages.sd_setImage(with: URL(string: snapshot.value as! String), placeholderImage: #imageLiteral(resourceName: "Menu"), options: [.continueInBackground, .progressiveDownload]) 
     }) 
     cell.FrontpageImages.contentMode = .scaleAspectFill 
     cell.FrontpageImages.layer.cornerRadius = 5 
     cell.FrontpageImages.clipsToBounds = true 
     return cell 
    } 

    } 
    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

    } 

//Here's where the data goes into the second view controller 

    import UIKit 
    import Firebase 
    import FirebaseDatabase 


    class Testing: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 
    var ref: DatabaseReference! 
    var DatabaseHandle = nil as DatabaseHandle! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

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

    //Populate view 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cells", for: indexPath) as! myCell 
     self.DatabaseHandle = ref.child(yourVariable).child(ASIN).child("VariationImages").child(String(indexPath.row)).observe(.value, with: { (snapshot) in 
      cell.myImageViews.sd_setImage(with: URL(string: snapshot.value as! String), placeholderImage: #imageLiteral(resourceName: "Menu"), options: [.continueInBackground, .progressiveDownload]) 


      }) 
     return cell 
} 

无处不在DataBaseHandle是,产生一个错误,因为它是一个空字符串,因为变量没有更新

+0

请显示崩溃发生的代码。 – Paulw11

+0

我编辑了帖子并添加了您请求的代码,感谢您回复@ Paulw11 –

+0

您可以发布Govind的代码吗?由于这是崩溃,你应该发布它。 – ryantxr

回答

1

最有可能的是越来越执行您的功能CallGovind()调用完成处理程序之前完成。这意味着您的其他视图控制器在之前被设置为

... .observe(.value, with: { (TheCategory) in 
    // Code in here will get executed asynchronously 
    // and likely will get called later 
    // Since you set variables in here, and they 
    // are not set by the time you call the other 
    // view controller. 
} 

CallGovind() // gets called BEFORE above code 

一种可能的解决方案是确保完成块完成后调用另一个视图控制器。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    self.DatabaseHandle = ref.child("Frontpage").child(String(indexPath.row)).observe(.value, with: { (TheCategory) in 
     yourVariable = TheCategory.childSnapshot(forPath: "Category").value as! String 
     ASIN = TheCategory.childSnapshot(forPath: "ASIN").value as! String 
     self.DatabaseHandle = self.ref.child(TheCategory.childSnapshot(forPath: "Category").value as! String).child(TheCategory.childSnapshot(forPath: "ASIN").value as! String).child("VariationImages").observe(.value, with: { (NumberOfVariationImages) in 
      VariationImages = Int(NumberOfVariationImages.childrenCount) 
      // Call other controller here so that the variables 
      // are set 
      DispatchQueue.main.async { 
       CallGovind() 
      } 
     }) 
    }) 

} 
+1

谢谢@ryantxr完美的代码。谢谢你,我有这个问题的日子。非常感谢!!! –

+0

我建议你看看在控制器之间传递数据的其他方法。你可以看看[传奇马铃薯](https://github.com/ryantxr/legendary-potato)。 – ryantxr

+0

非常感谢@ryantxr,我从心底深深地感受到了所有的帮助,我第一次使用Stackoverflow,并且展现出很强的社区感。再次感谢你 –

0

AS ryantxr在闭包内声明callGovind,并且所有类之间共享变量的好方法是singleTon。

按我的知识,你必须创建单独的类来管理这种类型的变量,如创建单独的单文件

import Foundation 
/** 
* Created by Jaydeep on 13-Feb-17. 
*/ 

public class Singleton 
{ 
    var yourVariable : String = "" 
    static let shared = Singleton() 
    private init() 
    { 
    } 
} 

使用

Singleton.shared.yourVariable = "xyz" 

你可以在任何地方,并随时访问Singleton类对象你会得到最新的更新值。

+0

谢谢你,我从来没有看过SingleTon类,但因为我有,似乎它会工作。谢谢你的帮助! @JaydeepVyas –

+0

这个变量没有更新到“公开课”为什么? –