我有一个分段控件来定义准备食谱的难度级别。我需要在一个配方中加载值分配,我不会编辑它。代码如下:
@IBOutlet var segNewDifficultyLevel: UISegmentedControl!
@IBOutlet var ingredientTableView: UITableView!
@IBOutlet var directionTableView: UITableView!
weak var delegate: AddNewRecipeViewControllerDelegate?
// MARK: - Variabili Globali
var editRecipe: RecipeModel!
var difficultyLevel: String!
var ingredients: [IngredientModel] = []
var directions: [DirectionModel] = []
此代码位于 viewDidLoad 中:
if let editToRecipe = editRecipe {
title = "Edit Recipe"
// Load data from Recipe Detail into editRecipe
viewImageNewRecipe.image = editToRecipe.imageRecipe
fieldNewRecipeName.text = editToRecipe.nameRecipe
fieldNewQuantityFor.text = editToRecipe.quantityRecipe
fieldNewPrepareTime.text = editToRecipe.preparationTime
fieldNewCookingTime.text = editToRecipe.cookingTime
fieldNewBakingTemp.text = editToRecipe.bakingTempRecipe
/* Load the Difficulty Level selected in the recipe */
segNewDifficultyLevel.selectedSegmentIndex = ???
有人可以帮我吗? 非常感谢!!
配方模型类别:
class RecipeModel: NSObject, NSCoding
{
var nameRecipe: String
var quantityRecipe: String
var recipeTime: String
var preparationTime: String
var cookingTime: String
var bakingTempRecipe: String
var difficultyLevelRecipe: String
var imageRecipe: UIImage
var ingredients: [IngredientModel]
var directions: [DirectionModel]
var recipeTimeInt: Int
{
var sumRecipeTime: Int = Int(recipeTime)!
sumRecipeTime = Int(preparationTime)! + Int(cookingTime)!
return sumRecipeTime
}
init(nameRecipe: String, quantityRecipe: String, recipeTime: String, preparationTime: String, cookingTime: String, bakingTempRecipe: String, difficultyLevelRecipe: String, imageRecipe: UIImage, ingredients: [IngredientModel], directions: [DirectionModel]) {
self.nameRecipe = nameRecipe
self.quantityRecipe = quantityRecipe
self.recipeTime = recipeTime
self.preparationTime = preparationTime
self.cookingTime = cookingTime
self.bakingTempRecipe = bakingTempRecipe
self.difficultyLevelRecipe = difficultyLevelRecipe
self.imageRecipe = imageRecipe
self.ingredients = ingredients
self.directions = directions
}
。 }
如果您在difficultyLevelRecipe
中将索引作为字符串获取,则可以将其转换为Int
,如selectedSegmentIndex
设置的那样,如下所示:
segNewDifficultyLevel.selectedSegmentIndex = Int(editToRecipe.difficultyLevelRecipe)
但是,如果您获得其他值,例如作为difficultyLevelRecipe
的值/名称,则需要根据segNewDifficultyLevel
中可用的值/名称进行设置。您需要通过比较段名称和difficultyLevelRecipe
来根据条件对其进行管理。
我希望你有清晰的想法并明白该怎么做。