SwiftUI #State wierd wrappedValue behaviour - swiftui

So straight to the point i have this code:
struct DataModel {
var option: String
}
struct ViewModel {
var selected: String? = "1"
var options = [DataModel(option: "1"), DataModel(option: "2"), DataModel(option: "3")]
}
struct ContentView: View {
#State var viewModel: ViewModel
var body: some View {
VStack {
Picker("test", selection: aBinder()) {
ForEach(viewModel.options, id: \.option) { option in
Text(option.option)
}
}
.background(Color.red)
}
}
func aBinder() -> Binding<String?> {
Binding<String?> {
viewModel.selected
} set: { value in
$viewModel.selected.wrappedValue = value
print($viewModel.selected.wrappedValue)
}
}
}
The value of "selected" in the viewModel doesn't change.
This works:
struct DataModel {
var option: String
}
struct ViewModel {
var selected: String = "1"
var options = [DataModel(option: "1"), DataModel(option: "2"), DataModel(option: "3")]
}
struct ContentView: View {
#State var viewModel: ViewModel
var body: some View {
VStack {
Picker("test", selection: $viewModel.selected) {
ForEach(viewModel.options, id: \.option) { option in
Text(option.option)
}
}
Button("press me", action: { print(viewModel.selected) })
}
}
}
But that doesn't make any sense. in both cases i use a binding to store the current value. What is going on? I'm pretty new to swiftUI so i might have missed how something works.
Thanks in advance

The types don’t match selected is a String and the options are a DataModel. The types have to match exactly.

Related

sheet does not dismiss on swiftUI

#Environment(\.presentationMode) var presentationMode
#Environment(\.dismiss) var dismiss
I know just toggle State variable of 'fullScreenCover' or 'sheet' but I found another approach and both above doesn't work.
Here's simple code.
struct ContentView: View {
#Environment(\.presentationMode) var presentationMode
#Environment(\.dismiss) var dismiss
#State var sheetState = false
#State private var value = ""
var body: some View {
VStack {
Text(value)
Button("Present Sheet") {
sheetState.toggle()
}
}
.sheet(isPresented: $sheetState) {
Button("set1") {
value = "1"
//presentationMode.wrappedValue.dismiss()
dismiss()
}
}
}
}
When I press the button of sheet named 'set1' it change the 'value' set '1' and Text of VStack set "1" correctly but sheet doesn't dismiss.
It absolutely same both approach.
Am I using these things wrong?
here's my result.
https://imgur.com/a/5cjExyv
Technically you are trying to dismiss ContentView which makes no sense.
You have to create a separate View struct for the sheet.
struct ContentView: View {
#State private var sheetState = false
#State private var value = ""
var body: some View {
VStack {
Text(value)
Button("Present Sheet") {
sheetState.toggle()
}
}
.sheet(isPresented: $sheetState) {
SheetView(value: $value)
}
}
}
struct SheetView : View {
#Environment(\.dismiss) var dismiss
#Binding var value : String
var body: some View {
Button("set1") {
value = "1"
dismiss()
}
}
}

SwiftUI: How to set toggle isOn in a foreach loop

Hi I have a for each loop which goes through a struct with values and in this for each loop I am trying to create a toggle for each and each needs its own isOn parameter so in my struct I have added 'isToggleOn' and set it to true but i am getting the following error: Cannot convert value of type 'Bool' to expected argument type 'Binding<Bool>'
and this view gets called by another view which provides it with the Sandwich struct
And i am not sure what the best way to approach this and any help would be appreciated.
import SwiftUI
struct IngredientView: View {
var sandwich: Sandwich
var body: some View {
ForEach(sandwich.ingredients) { ingredient in
HStack {
Text(ingredient.name)
Toggle("", isOn: ingredient.isToggleOn)
.toggleStyle(CheckboxToggleStyle(style: .square))
.foregroundColor(.blue)
}
}
}
you could try something like this:
struct IngredientView: View {
#Binding var sandwich: Sandwich
var body: some View {
ForEach($sandwich.ingredients) { $ingredient in
HStack {
Text(ingredient.name)
Toggle("", isOn: $ingredient.isToggleOn)
.toggleStyle(CheckboxToggleStyle(style: .square))
.foregroundColor(.blue)
}
}
}
}
With , for example,
struct ContentView: View {
#State var sandwich = Sandwich()
var body: some View {
IngredientView(sandwich: $sandwich)
.onAppear {
sandwich.ingredients = [
Ingredient(isToggleOn: false, name: "ingredient-1"),
Ingredient(isToggleOn: true, name: "ingredient-2"),
Ingredient(isToggleOn: false, name: "ingredient-3")]
}
}
}
assuming something like this:
struct Ingredient: Identifiable, Codable {
let id = UUID()
var isToggleOn: Bool = false
var name: String = ""
}
struct Sandwich: Codable {
var ingredients: [Ingredient] = []
}

How can I have multiple instance of a Class/Model in SwiftUI?

The first part of question is answered. Let's elaborate this example to:
TextField view:
struct CreateNewCard: View {
#ObservedObject var viewModel: CreateNewCardViewModel
var body: some View {
TextField("placeholder...", text: $viewModel.definition)
.foregroundColor(.black)
}
}
ViewModel:
class CreateNewCardViewModel: ObservableObject {
#Published var id: Int
#Published var definition: String = ""
}
Main View:
struct MainView: View {
#State var showNew = false
var body: some View {
ForEach(0...10, id: \.self) { index in // <<<---- this represents the id
Button(action: { showNew = true }, label: { Text("Create") })
.sheet(isPresented: $showNew, content: {
// now I have to pass the id, but this
// leads to that I create a new viewModel every time, right?
CreateNewCard(viewModel: CreateNewCardViewModel(id: index))
})
}
}
My problem is now that when I type something into the TextField and press the return button on the keyboard the text is removed.
This is the most strange way of coding that i seen, how ever I managed to make it work:
I would like say that you can use it as leaning and testing, but not good plan for real app, How ever it was interesting to me to make it working.
import SwiftUI
struct ContentView: View {
var body: some View {
MainView()
}
}
class CreateNewCardViewModel: ObservableObject, Identifiable, Equatable {
init(_ id: Int) {
self.id = id
}
#Published var id: Int
#Published var definition: String = ""
#Published var show = false
static func == (lhs: CreateNewCardViewModel, rhs: CreateNewCardViewModel) -> Bool {
return lhs.id == rhs.id
}
}
let arrayOfModel: [CreateNewCardViewModel] = [ CreateNewCardViewModel(0), CreateNewCardViewModel(1), CreateNewCardViewModel(2),
CreateNewCardViewModel(3), CreateNewCardViewModel(4), CreateNewCardViewModel(5),
CreateNewCardViewModel(6), CreateNewCardViewModel(7), CreateNewCardViewModel(8),
CreateNewCardViewModel(9) ]
struct ReadModelView: View {
#ObservedObject var viewModel: CreateNewCardViewModel
var body: some View {
TextField("placeholder...", text: $viewModel.definition)
.foregroundColor(.black)
}
}
struct MainView: View {
#State private var arrayOfModelState = arrayOfModel
#State private var showModel: Int?
#State private var isPresented: Bool = false
var body: some View {
VStack {
ForEach(Array(arrayOfModelState.enumerated()), id:\.element.id) { (index, item) in
Button(action: { showModel = index; isPresented = true }, label: { Text("Show Model " + item.id.description) }).padding()
}
if let unwrappedValue: Int = showModel {
Color.clear
.sheet(isPresented: $isPresented, content: { ReadModelView(viewModel: arrayOfModelState[unwrappedValue]) })
}
}
.padding()
}
}

list not updating after texts change

i am sure i am making something wrong, but i don't know what.
I have a small list and i update texts with a timer which i can see in the debugger which updates. But the list won't be updated...
Thank you for help.
struct ListTest: View {
#State var texts : [String]
var body: some View {
List {
ForEach(self.texts, id: \.self) { text in
Text(text)
}
}
}
}
struct ContentView: View {
#State var texts = ["a"]
var body: some View {
ListTest(texts: self.texts)
.onAppear() {
Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { (timer) in
self.texts.append("\(self.texts.count)")
print(self.texts)
}
}
}
}
remove #State property wrapper from your ListTest
struct ListTest: View {
var texts : [String]
var body: some View {
List {
ForEach(self.texts, id: \.self) { text in
Text(text)
}
}
}
}
Finally, think about more "combine compatible" code
import SwiftUI
struct ListTest: View {
var texts: [String]
var body: some View {
List {
ForEach(self.texts, id: \.self) { text in
Text(text)
}
}
}
}
struct ContentView: View {
#State var texts = ["a"]
let tp = Timer.publish(every: 0.5, on: .main, in: .default).autoconnect()
var body: some View {
ListTest(texts: self.texts)
.onReceive(tp) { (date) in
self.texts.append("\(self.texts.count)")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
which is functionally equal

how to use a #EnvironmentObject in combination with a List

The code for the basic app from Anlil's answer works fine. If I edit the datamodel to be more like mine, with a multidimensional String array, I get something like:
import SwiftUI
import Combine
struct ContentView: View {
#EnvironmentObject var dm: DataManager
var body: some View {
NavigationView {
List {
NavigationLink(destination:AddView().environmentObject(self.dm)) {
Image(systemName: "plus.circle.fill").font(.system(size: 30))
}
ForEach(dm.array, id: \.self) { item in
NavigationLink(destination: DetailView(item: item)) {
Text(item[0])
}
}
}
}
}
}
struct DetailView: View {
var item : [String] = ["", "", ""]
var body: some View {
VStack {
Text(item[0])
Text(item[1])
Text(item[2])
}
}
}
struct AddView: View {
#EnvironmentObject var dm: DataManager
#State var item0 : String = "" // needed by TextField
#State var item1 : String = "" // needed by TextField
#State var item2 : String = "" // needed by TextField
#State var item : [String] = ["", "", ""]
var body: some View {
VStack {
TextField("Write something", text: $item0)
.textFieldStyle(.roundedBorder)
.padding(.horizontal)
TextField("Write something", text: $item1)
.textFieldStyle(.roundedBorder)
.padding(.horizontal)
TextField("Write something", text: $item2)
.textFieldStyle(.roundedBorder)
.padding(.horizontal)
Button(action: {
self.item = [self.item0, self.item1, self.item2]
print(self.item)
self.dm.array.append(self.item)
}) {
Text("Save")
}
}
}
}
class DataManager: BindableObject {
var willChange = PassthroughSubject<Void, Never>()
var array : [[String]] = [["Item 1","Item 2","Item 3"],["Item 4","Item 5","Item 6"],["Item 7","Item 8","Item 9"]] {
didSet {
willChange.send()
}
}
}
There are no errors and the code runs as expected. Before I'm going to rewrite my own code (with the lessons I've learned solar) it would be nice if the code could be checked.
I'm really impressed with SwiftUI!
If your "source of truth" is an array of some "model instances", and you just need to read values, you can pass those instance around like before:
import SwiftUI
import Combine
struct ContentView: View {
#EnvironmentObject var dm: DataManager
var body: some View {
NavigationView {
List(dm.array, id: \.self) { item in
NavigationLink(destination: DetailView(item: item)) {
Text(item)
}
}
}
}
}
struct DetailView: View {
var item : String
var body: some View {
Text(item)
}
}
class DataManager: BindableObject {
var willChange = PassthroughSubject<Void, Never>()
let array = ["Item 1", "Item 2", "Item 3"]
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environmentObject(DataManager())
}
}
#endif
You need to pass the EnvironmentObject only if some views are able to manipulate the data inside the instances... in this case you can easily update the EnvironmentObject's status and everything will auto-magically updated everywhere!
The code below shows a basic App with "list", "detail" and "add", so you can see 'environment' in action (the only caveat is that you have to manually tap < Back after tapped the Save button). Try it and you'll see the list that will magically update.
import SwiftUI
import Combine
struct ContentView: View {
#EnvironmentObject var dm: DataManager
var body: some View {
NavigationView {
List {
NavigationLink(destination:AddView().environmentObject(self.dm)) {
Image(systemName: "plus.circle.fill").font(.system(size: 30))
}
ForEach(dm.array, id: \.self) { item in
NavigationLink(destination: DetailView(item: item)) {
Text(item)
}
}
}
}
}
}
struct DetailView: View {
var item : String
var body: some View {
Text(item)
}
}
struct AddView: View {
#EnvironmentObject var dm: DataManager
#State var item : String = "" // needed by TextField
var body: some View {
VStack {
TextField("Write something", text: $item)
.textFieldStyle(.roundedBorder)
.padding(.horizontal)
Button(action: {
self.dm.array.append(self.item)
}) {
Text("Save")
}
}
}
}
class DataManager: BindableObject {
var willChange = PassthroughSubject<Void, Never>()
var array : [String] = ["Item 1", "Item 2", "Item 3"] {
didSet {
willChange.send()
}
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environmentObject(DataManager())
}
}
#endif