[英]fetching textfield.text values from dynamically generated textfields
[英]Firebase restoring initial values in textfields after changing text value
我正在使用Firebase練習iOS(Swift)。 第一個viewcontroller從firebase db檢索所有記錄,並從數組填充tableView。 當用戶從該表視圖中選擇一個項目時,將彈出一個新的視圖控制器,將對象從listView視圖控制器連接到詳細信息視圖控制器。 數據已成功填充到字段中!
但是,當我嘗試更新任何文本字段時,當我切換到另一個文本字段時,初始值將在已編輯的文本字段中恢復。
我曾嘗試刪除allObservers ...但沒有任何效果。 我什至刪除了“導入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)";
}
}
}
謝謝大家的幫助。
可能是因為您將填充textField的代碼放在viewDidLayoutSubviews()方法中。 每當viewcontroller中的視圖布局更改時,都會觸發此方法。
將其移動到viewDidLoad(),應該沒問題。
之所以將其還原為先前的值,是因為。 您正在從“成分”對象中填充textField.text。 該成分將保留與您從以前的視圖控制器傳遞的值相同的值。 直到您將其變異。
而且,Firebase非常酷,我也很喜歡:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.