ios - 如何拥有一个视图控制器来更改多个 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)
}
}
解决方案
推荐阅读
- ubuntu - Ubuntu 18.04 GNOME Nautilus 缺少 ~/Templates 目录
- javascript - js框架只加载一次,被其他请求多次重用
- apollo-server - Lerna 找不到符号链接模块的对等依赖项
- java - 在远程集群上运行 Flink 作业而不提供 .jar
- testing - 如何设置 TestCafe 以在 CLI 中指定的环境中运行?
- gcloud - gcloud dataproc 集群创建:没有名为 jsonschema 的模块
- python - 用于传输照片的python套接字编程
- matlab - 太多 for 循环迭代 - for 循环终止
- java - 如何在数组中获得最小值
- coq - 如何在 Coq Software Foundations 中查找关键字的解释?