我正在使用Firebase练习iOS(Swift)。第一个viewcontroller从firebase db中检索所有记录,并从数组中填充tableView。当用户从该tableview中选择一个项目时,会弹出一个新的viewcontroller,将该对象从listView视图控制器转移到detail viewcontroller。数据已成功填充到字段中!
然而,当我尝试更新任何文本字段时,当我切换到另一个文本字段时,初始值将在已编辑的文本字段中恢复。
我试图删除AllObservers ...但没有任何效果。我甚至删除了#34;导入Firebase"和所有相关的对象仍然恢复初始值。
我在这里错过了任何概念吗?这是ListViewController的代码:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated);
self.activityIndicator.startAnimating();
self.observeIngrdUpdates = ref.observeEventType(.Value, withBlock: { (snapshot) in
self.ingredients.removeAll();
for child in snapshot.children {
var nutrition:String = "";
var type:Int = 0;
var desc:String = "";
var img:String = "";
var name:String = "";
var price:Double = 0.0;
if let _name = child.value["IngredientName"] as? String {
name = _name;
}
if let _nutrition = child.value["NutritionFacts"] as? String {
nutrition = _nutrition;
}
if let _type = child.value["IngredientType"] as? Int {
type = _type;
}
if let _desc = child.value["UnitDescription"] as? String {
desc = _desc;
}
if let _img = child.value["IngredientImage"] as? String {
img = _img;
}
if let _price = child.value["IngredientPrice"] as? Double {
price = _price;
}
let ingredient = Ingredient(name: name, type: type, image: img, unitDesc: desc, nutritionFacts: nutrition, price: price);
ingredient.key = child.key;
self.ingredients.append(ingredient);
}
self.tableView.reloadData();
})
}
和PrepareForSegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController as! EditIngredientVC;
if segue.identifier?.compare(SEGUE_EDIT_INGREDIENTS) == .OrderedSame {
destinationVC.ingredient = ingredients[tableView.indexPathForSelectedRow!.row];
destinationVC.controllerTitle = "Edit";
} else if segue.identifier?.compare(SEGUE_ADD_INGREDIENT) == .OrderedSame {
destinationVC.controllerTitle = "New";
}
}
这是填充DetailViewController中字段的代码:
override func viewDidLayoutSubviews() {
lblControllerTitle.text = controllerTitle;
if controllerTitle?.localizedCaseInsensitiveCompare("NEW") == .OrderedSame {
self.segVegFru.selectedSegmentIndex = 0;
} else {
if ingredient != nil {
self.txtIngredientName.text = ingredient!.name;
self.txtUnitDesc.text = ingredient!.unitDesc;
self.segVegFru.selectedSegmentIndex = ingredient!.typeInt;
self.txtNutritionFacts.text = ingredient!.nutritionFacts;
self.txtPrice.text = "\(ingredient!.price)";
}
}
}
谢谢大家的帮助。
答案 0 :(得分:0)
这可能是因为你在viewDidLayoutSubviews()方法中放置了textField填充代码。 每次在viewcontroller中视图布局发生变化时,都会触发此方法。
将其移至viewDidLoad(),它应该没问题。
它被恢复到之前的值的原因是因为。您正在填充“ingredient”对象中的textField.text。该成分将保留与从前一个视图控制器传递的相同值。直到你在某个时候改变它。
顺便说一句,Firebase非常酷,我也喜欢它。)