Post

Replies

Boosts

Views

Activity

Reply to Deleting a View Instance with the Tap of a Button
I got it thanks to Claude31. import SwiftUI struct ContentView: View { @ObservedObject var monster: Monster var body: some View { VStack { Button { monster.items.append(Keyword()) } label: { Text("Add me!") }.padding(.vertical, 10.0) ForEach(monster.items) { item in KeywordRow(id: item.id).environmentObject(monster) } } } } struct Keyword: Identifiable { let id = UUID() } struct KeywordRow: View { @State var id = UUID() @EnvironmentObject var monster: Monster var body: some View { VStack { HStack { Text("ID: \(id)") Button { monster.items.removeAll(where: { $0.id == id} ) } label: { Text("Delete") } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’22
Reply to TextField Binding
Well, I guess I could do the following. import SwiftUI struct ContentView: View { @State private var names: [String] = ["Jim Thorton", "Susan Murphy", "Tom O'Donnell", "Nancy Smith"] var body: some View { HStack { ForEach($names, id: \.self) { $name in TextField("", text: $name) .fixedSize() } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’22
Reply to Showing Multiple Instances of View and Showing Results from Them
I guess it's something close to the following. struct ContentView: View { @State var textMonsters = [MyTextView]() var body: some View { VStack { Button { print("Monsters \(textMonsters)") } label: { Text("Show me your current text strings") }.padding(.vertical, 10.0) ForEach(textMonsters, id: \.self) { textMonster in textMonster }.padding(.horizontal, 100.0) Button { textMonsters.append(MyTextView(id: UUID(), text: "ABC")) } label: { Text("Add me!") }.padding(.vertical, 10.0) } } } struct MyTextView: View, Identifiable, Hashable { var id = UUID() var text: String var body: some View { ZStack { TextField("Enter some text", text: Binding(get: { text }, set: { _ in })) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’22
Reply to Showing Multiple Instances of View and Showing Results from Them
I've figured it out accidentally. import SwiftUI struct ContentView: View { @ObservedObject var monster: Monster var body: some View { VStack { Button { print(monster.items) } label: { Text("Show me your current text strings") }.padding(.vertical, 10.0) ForEach($monster.items) { item in MyTextView(id: item.id, text: item.text).environmentObject(monster) }.padding(.horizontal, 200.0) Button { monster.items.append(Keyword()) } label: { Text("Add me!") }.padding(.vertical, 10.0) } } } class Monster: ObservableObject { @Published var items = [Keyword]() } struct Keyword: Identifiable { var id = UUID() var text = String() } struct MyTextView: View { @Binding var id: UUID @Binding var text: String @EnvironmentObject var monster: Monster var body: some View { VStack { HStack { TextField("Enter text", text: $text) Button { monster.items.removeAll(where: { $0.id == id} ) } label: { Text("Delete") } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’22
Reply to Binding<String>, set, get?
The following would work except that I'm required to use Binding. struct ContentView: View { @State var username = "" @State var password = "" @State var tenantID = "" var body: some View { VStack { makeForm(label: "Username: ", placeHolder: "123456", text: $username) makeForm(label: "Password: ", placeHolder: "abcdefg", text: $password) //makeForm(label: "Shop ID: ", placeHolder: "Amazon River Branch", text: $tenantID) HStack { Text("Shop ID: ") TextField("Amazon River Branch", text: $tenantID) .onChange(of: tenantID) { newValue in tenantID = textFilter(value: newValue) } } }.padding(.horizontal, 40.0) } @ViewBuilder private func makeForm(label: String, placeHolder: String, text: Binding<String>) -> some View { HStack { let newText = Binding<String>( get: { text.wrappedValue }, set: { text.wrappedValue = textFilter(value: $0) } ) Text(label) TextField(placeHolder, text: newText) } } func textFilter(value: String) -> String { let pattern = "[^A-Za-z0-9]+" return value.replacingOccurrences(of: pattern, with: "", options: [.regularExpression]) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’22
Reply to Showing a Constructor Dialog View with an ObservedObject Object
I guess the user State object in ContentView comes with default values.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to Deleting a View Instance with the Tap of a Button
I got it thanks to Claude31. import SwiftUI struct ContentView: View { @ObservedObject var monster: Monster var body: some View { VStack { Button { monster.items.append(Keyword()) } label: { Text("Add me!") }.padding(.vertical, 10.0) ForEach(monster.items) { item in KeywordRow(id: item.id).environmentObject(monster) } } } } struct Keyword: Identifiable { let id = UUID() } struct KeywordRow: View { @State var id = UUID() @EnvironmentObject var monster: Monster var body: some View { VStack { HStack { Text("ID: \(id)") Button { monster.items.removeAll(where: { $0.id == id} ) } label: { Text("Delete") } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to TextField Binding
Well, I guess I could do the following. import SwiftUI struct ContentView: View { @State private var names: [String] = ["Jim Thorton", "Susan Murphy", "Tom O'Donnell", "Nancy Smith"] var body: some View { HStack { ForEach($names, id: \.self) { $name in TextField("", text: $name) .fixedSize() } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to Horizontally-Aligned TextField Wrap?
The following Stack Overflow could be a solution that I've been looking for. https://stackoverflow.com/questions/58842453/swiftui-hstack-with-wrap
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to Showing Multiple Instances of View and Showing Results from Them
I need to change MyTextView() to MyTextView(text: "Hello")
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to Showing Multiple Instances of View and Showing Results from Them
I guess it's something close to the following. struct ContentView: View { @State var textMonsters = [MyTextView]() var body: some View { VStack { Button { print("Monsters \(textMonsters)") } label: { Text("Show me your current text strings") }.padding(.vertical, 10.0) ForEach(textMonsters, id: \.self) { textMonster in textMonster }.padding(.horizontal, 100.0) Button { textMonsters.append(MyTextView(id: UUID(), text: "ABC")) } label: { Text("Add me!") }.padding(.vertical, 10.0) } } } struct MyTextView: View, Identifiable, Hashable { var id = UUID() var text: String var body: some View { ZStack { TextField("Enter some text", text: Binding(get: { text }, set: { _ in })) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to Showing Multiple Instances of View and Showing Results from Them
@Claude31 No, sir.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to Showing Multiple Instances of View and Showing Results from Them
I've figured it out accidentally. import SwiftUI struct ContentView: View { @ObservedObject var monster: Monster var body: some View { VStack { Button { print(monster.items) } label: { Text("Show me your current text strings") }.padding(.vertical, 10.0) ForEach($monster.items) { item in MyTextView(id: item.id, text: item.text).environmentObject(monster) }.padding(.horizontal, 200.0) Button { monster.items.append(Keyword()) } label: { Text("Add me!") }.padding(.vertical, 10.0) } } } class Monster: ObservableObject { @Published var items = [Keyword]() } struct Keyword: Identifiable { var id = UUID() var text = String() } struct MyTextView: View { @Binding var id: UUID @Binding var text: String @EnvironmentObject var monster: Monster var body: some View { VStack { HStack { TextField("Enter text", text: $text) Button { monster.items.removeAll(where: { $0.id == id} ) } label: { Text("Delete") } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to Binding<String>, set, get?
Currently, what I type is not filtered.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’22
Reply to Binding<String>, set, get?
Currently, the warning says "Expression of type 'String' is unused."
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’22
Reply to Binding<String>, set, get?
The warning will disappear with the following, but the text entry won't be filtered. set: { text.wrappedValue = filter(value: $0) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’22
Reply to Binding<String>, set, get?
I think I cannot use Binding. Instead, I think I need to use TextField with .onChange.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’22
Reply to Binding<String>, set, get?
The following would work except that I'm required to use Binding. struct ContentView: View { @State var username = "" @State var password = "" @State var tenantID = "" var body: some View { VStack { makeForm(label: "Username: ", placeHolder: "123456", text: $username) makeForm(label: "Password: ", placeHolder: "abcdefg", text: $password) //makeForm(label: "Shop ID: ", placeHolder: "Amazon River Branch", text: $tenantID) HStack { Text("Shop ID: ") TextField("Amazon River Branch", text: $tenantID) .onChange(of: tenantID) { newValue in tenantID = textFilter(value: newValue) } } }.padding(.horizontal, 40.0) } @ViewBuilder private func makeForm(label: String, placeHolder: String, text: Binding<String>) -> some View { HStack { let newText = Binding<String>( get: { text.wrappedValue }, set: { text.wrappedValue = textFilter(value: $0) } ) Text(label) TextField(placeHolder, text: newText) } } func textFilter(value: String) -> String { let pattern = "[^A-Za-z0-9]+" return value.replacingOccurrences(of: pattern, with: "", options: [.regularExpression]) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’22
Reply to Files Selected with UIDocumentPickerViewController Ending Up in File Provider Storage [SwiftUI]
It sounds like I've screwed up a big time according to RayWenderlich's article. https://www.raywenderlich.com/697468-ios-file-provider-extension-tutorial
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Jul ’22
Reply to Deleting a Row in the List with Data from Realm
when we use access a dataset in Realm -> when we access a dataset in Realm
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’22