首页 > 解决方案 > 如何拥有一个视图控制器来更改多个 UIImageView 和 UITextView?

问题描述

我正在创建一个包含多个不同食谱的烹饪应用程序。由于我需要为每个菜谱/餐点创建一个视图控制器,因此会复制很多视图,例如 imageview、textview 等。我可以有一个视图来交换菜谱吗?我将如何调用特定的食谱及其图像、成分、标题等?顺便说一句,我是 Swift 的新手,所以如果有人能解释一下如何做到这一点?

更新:

这就是我到目前为止所拥有的。我如何将一个特定的食谱返回给BasicBreakfastViewController?

class Recipes: NSObject {
    var recipeName = ""
    //name of recipe 
    var prepTime = ""
    //preparation time 
    var imageFile = ""
    //image filename of recipe
    var ingredients: [Any] = []
    // ingredients
    var step1 = ""
    var step2 = ""
    //recipe directions
    var calories: Int = 0  
}
//BreakfastRecipesViewController
class BreakfastRecipesViewController: UIViewController{
    var recipe: Recipes?

    @IBOutlet weak var recipeImage: UIImageView!
    @IBOutlet weak var prepTimeLabel: UILabel!
    @IBOutlet weak var caloriesLabel: UILabel!
    @IBOutlet weak var recipeTitle: UILabel! 
    @IBOutlet weak var directionStep1: UILabel!
    @IBOutlet weak var directionStep2: UILabel!
    @IBOutlet weak var ingredientsTableView: UITableView!
    @IBOutlet weak var addToListBtn: UIButton!
    @IBAction func handleSwipe(_ sender: Any) {

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let riceCakeRecipe = Recipes()
        riceCakeRecipe.recipeName = "PEANUT BUTTER AND BANANA RICE CAKE"
        riceCakeRecipe.prepTime = "5 Minutes"
        riceCakeRecipe.imageFile = "IMG_273711.JPG"
        //riceCakeRecipe.calories = 281
        riceCakeRecipe.ingredients = ["• 2 Rice Cakes", "• 2 Tbsp Peanut Butter", "• 1/2 Banana"]
        riceCakeRecipe.step1 = "Distribute peanut butter evenly onto the rice cakes."
        riceCakeRecipe.step2 = "Chop half of a banana and place on top of peanut butter."

        let proteinBarkRecipe = Recipes()
        proteinBarkRecipe.recipeName = "FROZEN YOGURT PROTEIN BARK"
        proteinBarkRecipe.prepTime = "15 Minutes"
        proteinBarkRecipe.imageFile = "IMG_298611.JPG"
        //riceCakeRecipe.calories = 364
        proteinBarkRecipe.ingredients = ["• 1 Cup Greek Yogurt", "• 32g Vanilla Protein Powder", "• 5 Large Fresh Strawberries", "• 1 Tbsp Dark Chocolate", "• 1 Tsp Coconut Flakes"]
        proteinBarkRecipe.step1 = "Mix the yogurt and protein powder together until well combined."
        proteinBarkRecipe.step2 = "Line a baking sheet with foil and spread a thick layer of the yogurt mixture on top."

       var recipes = [riceCakeRecipe, proteinBarkRecipe]



        recipeTitle.text = recipe?.recipeName
        prepTimeLabel.text = recipe?.prepTime
        recipeImage.image = UIImage(named: recipe!.imageFile)
        directionStep1.text = recipe?.step1
        directionStep2.text = recipe?.step2
        //caloriesLabel.text = recipe?.calories

        return

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

    }


}


标签: iosswiftview

解决方案


推荐阅读