通过Push Segue Back Button

时间:2016-05-17 03:09:53

标签: swift uitableview segue

我会尽力阐明我的问题和问题,并希望提供足够的代码来提供帮助。我正在创建一个配方应用程序,其中初始屏幕是一个桌面视图,然后单击“添加按钮”,弹出“添加配方视图控制器”,然后在文本字段中输入配方信息(名称,烹饪时间,准备时间)等等......)你点击“保存”,它有一个Unwind Segue回到TableView,它添加了新的食谱。初始表格视图准确显示配方名称,准备时间和类别。当您单击特定配方(tableview行)时,它会执行“配方详细信息视图控制器”的推送,显示您在“添加配方”视图控制器中输入的所有标签。我现在的问题是,当我想编辑这些信息时。我已经能够点击“编辑”并返回到“添加配方视图控制器”,您可以在其中编辑信息并更改所需的任何信息。当我单击“保存”时,它会返回“配方详细信息视图控制器”,并且所有信息都会准确更改。我的问题和问题是我如何制作它以便保存回表视图,所以当我从推送segue单击后退按钮时,tableview信息反映了更改并保存了所有内容。

这是我的代码,当用户点击保存编辑配方并关闭视图控制器并返回“配方详细信息视图控制器”

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if saveButton === sender {
        let name2 = recipeNameField.text ?? ""
        let photo2 = photoImageView.image
        let source2 = sourceField.text ?? ""
        let category2 = categoryField.text ?? ""
        let servings2 = servingsField.text ?? ""
        let prepTime2 = prepTimeField.text ?? ""
        let cookTime2 = cookTimeField.text ?? ""
        let sourceURL2 = sourceURLField.text ?? ""

        newRecipe = Recipe(name: name2, servings: servings2, category: category2, prepTime: prepTime2, cookTime: cookTime2, source: source2, sourceURL: sourceURL2, photo: photo2)
    }
}

现在我有一个Unwing segue从配方详细信息视图控制器转到表视图,它可以工作并更新表视图信息,但加载时间很长,有时会崩溃。当我从“编辑配方视图控制器”中点击“保存”并且只使用推送segue中的后退按钮时,我宁愿找到一种方法来保存它。

下面是我现在要回到TableView的Unwind segue代码

@IBAction func unwindToMyRecipesFromDetail(sender: UIStoryboardSegue) {
    if let sourceViewController = sender.sourceViewController as? RecipeDetailViewController, indrecipe = sourceViewController.recipeDetail {
        if let selectedIndexPath = tableView.indexPathForSelectedRow {
            // Update an existing meal.
            recipes2[selectedIndexPath.row] = indrecipe
            tableView.reloadRowsAtIndexPaths([selectedIndexPath], withRowAnimation: .None)
        } else {
            // Add a new meal.
            let newIndexPath = NSIndexPath(forRow: recipes2.count, inSection: 0)
            recipes2.append(indrecipe)
            tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
        }
    }
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if myRecipesButton === sender {
        let destVC = segue.destinationViewController as! RecipeListTableViewController

        let name2 = recipeNameLabel.text ?? ""
        let photo2 = recipeImage.image
        let source2 = sourceLabel.text ?? ""
        let category2 = categoryLabel.text ?? ""
        let servings2 = servingsLabel.text ?? ""
        let prepTime2 = prepTimeLabel.text ?? ""
        let cookTime2 = cookTimeLabel.text ?? ""
        let sourceURL2 = recipeDetail!.sourceURL ?? ""

        recipeDetail = Recipe(name: name2, servings: servings2, category: category2, prepTime: prepTime2, cookTime: cookTime2, source: source2, sourceURL: sourceURL2, photo: photo2)

        destVC.recipes3 = recipeDetail

    }
}

0 个答案:

没有答案