Get the uiSegmentControl value for edit


Get the uiSegmentControl value for edit



i have a segment control to define the difficulty level for the preparation a recipe. I need to load the value assign at one recipe that I won't to edit it. The follow the code:


@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] =



This code is in the 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 = ???



Anybody can help me please?
Thx a lot!!!



Model Class of Recipe:


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
}



...
}





can you share the model class / struct of Recipe ?
– S1LENT WARRIOR
Jun 30 at 18:42





Add Model Class. Thx for your attention...
– Davide
Jun 30 at 18:50




1 Answer
1



If you are getting index as string in you difficultyLevelRecipe then you can convert it to Int as set for selectedSegmentIndex like as below:


difficultyLevelRecipe


Int


selectedSegmentIndex


segNewDifficultyLevel.selectedSegmentIndex = Int(editToRecipe.difficultyLevelRecipe)



But, If you getting some other value like as value/name of difficultyLevelRecipe then you need to setup it based on value/name available in segNewDifficultyLevel. You need to manage it based on condition by comparing name of segment and your difficultyLevelRecipe.


difficultyLevelRecipe


segNewDifficultyLevel


difficultyLevelRecipe



I hope you have clear idea and understand what to do.





Thank you @Sagar, tomorrow i try your suggestion!
– Davide
Jun 30 at 21:27





Sorry @Sagar, your solution crash my app ... i’m search another solution...
– Davide
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API