【发布时间】:2018-06-03 09:51:22
【问题描述】:
我需要帮助。我的食谱应用程序有一个主视图控制器,其中有两个 UITableView(ingredientTableView 和 directionTableView)。每个都由“segue”从两个模态视图控制器填充。
数据存储在新配方的成分/方向数组中,但UITableView在关闭模式视图时不显示文本。
newRecipeController 的“保存”UIBarButton 代码:
// MARK: - IBActions
extension AddNewRecipeViewController {
// MARK: - Add Ingredient
@IBAction func saveIngredient(_ segue: UIStoryboardSegue)
{
guard let addIngredientModalViewController = segue.source as? AddIngredientModalViewController,
let ingredient = addIngredientModalViewController.ingredient else
{
return
}
// add the new ingredient to the ingredients array
ingredients.append(ingredient)
// update the tableview
let indexPath = IndexPath(row: ingredients.count - 1, section: 0)
ingredientTableView.insertRows(at: [indexPath], with: .automatic)
}
@IBAction func cancelAddIngredient(_ segue: UIStoryboardSegue)
{
dismiss(animated: true, completion: nil)
}
这是模态视图的 segue 代码准备:
// MARK: - Segue AddIngredient
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "SaveIngredient",
let ingredientName = fieldAddNameIngredient.text,
let ingredientValue = fieldValueOfIngredient.text
{
ingredient = IngredientModel(titleIngredientRecipe: ingredientName, subtitleIngredientRecipe: ingredientValue)
}
}
【问题讨论】:
-
您的
saveIngredient()函数将 segue 作为参数(它不应该这样做,因为它是一个按钮),但您从不使用 segue 调用它,所以segue.source可能是 nil由于guard语句,数据永远不会添加到表视图中。此外,您似乎没有提到主要的tableView。如果您关闭模态视图而不使用 segue,则不会调用prepareForSegue()。您可能需要从模态视图中找到主要的tableView,使用self.presentingViewController()之类的内容,然后将其附加到表格中。 -
另外,您是否将所有成分存储在主表视图控制器的数组中?
-
@Chris 感谢您的关注。成分以数组形式存储到主数组“NewRecipe”中。当我保存 newRecipe 时,detailController 会加载所有成分。
-
@Chris,我试着听从你的建议......但我是 Swift 的新手,我还有更多东西要学习......
-
查看您的故事板,您可能只需要展开转场将数据发送回主视图控制器,然后使用
prepareForSegue()。我现在不在电脑前,但我稍后会看看这个。
标签: ios swift uitableview uistoryboardsegue reloaddata