AnyView blocks View updates - swift

I have a simple view:
This view is wrapped by another view that has an observed object.
Such code doesn't update the view:
struct SomeView: View {
#ObservedObject var viewModel: ViewModel
var body: some View {
let text = viewModel.text
return ForEach(0..<1) { AnyView(text) }
}
}
but it works:
return ForEach(0..<1) { text }
Is it bug? Can I use AnyView for ForEach view?

Oh, when I asked a question I found a solution:
You should use ForEach ForEach(0..<1, id: \.self) { AnyView(text) }

Related

How to make a view gone in swiftui

I was trying to hide view but noticed that view.hidden() will only hide the view but but the space remains.
I also tried some of the suggestions on this link Dynamically hiding view in SwiftUI
But it didn't really work for me, albeit it was a starting point.
Find below what I finally did.
Here's an approach without an extension.
struct ContentView: View {
#State var hidden = false
#State var text = ""
var body: some View {
NavigationView {
List {
Section {
Toggle("Hide", isOn: $hidden.animation(Animation.easeInOut))
}
if !hidden {
Section {
TextField("Pin", text: $text)
}
}
}
}
}
}
I created an extension function as below
extension View {
#ViewBuilder func hiddenConditionally(isHidden: Binding<Bool>) -> some View {
isHidden.wrappedValue ? self : self.hidden() as? Self
}
}
and you can call it as below
TextField("Pin", text: $pin)
.hiddenConditionally(isHidden: $showPinField)

SwiftUI macOS NavigationView - onChange(of: Bool) action tried to update multiple times per frame

I'm seeing onChange(of: Bool) action tried to update multiple times per frame warnings when clicking on NavigationLinks in the sidebar for a SwiftUI macOS App.
Here's what I currently have:
import SwiftUI
#main
struct BazbarApp: App {
#StateObject private var modelData = ModelData()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(modelData)
}
}
}
class ModelData: ObservableObject {
#Published var myLinks = [URL(string: "https://google.com")!, URL(string: "https://apple.com")!, URL(string: "https://amazon.com")!]
}
struct ContentView: View {
#EnvironmentObject var modelData: ModelData
#State private var selected: URL?
var body: some View {
NavigationView {
List(selection: $selected) {
Section(header: Text("Bookmarks")) {
ForEach(modelData.myLinks, id: \.self) { url in
NavigationLink(destination: DetailView(selected: $selected) ) {
Text(url.absoluteString)
}
.tag(url)
}
}
}
.onDeleteCommand {
if let selected = selected {
modelData.myLinks.remove(at: modelData.myLinks.firstIndex(of: selected)!)
}
selected = nil
}
Text("Choose a link")
}
}
}
struct DetailView: View {
#Binding var selected: URL?
var body: some View {
if let selected = selected {
Text("Currently selected: \(selected)")
}
else {
Text("Choose a link")
}
}
}
When I alternate clicking on the second and third links in the sidebar, I eventually start seeing the aforementioned warnings in my console.
Here's a gif of what I'm referring to:
Interestingly, the warning does not appear when alternating clicks between the first and second link.
Does anyone know how to fix this?
I'm using macOS 12.2.1 & Xcode 13.2.1.
Thanks in advance
I think the issue is that both the List(selection:) and the NavigationLink are trying to update the state variable selected at once. A List(selection:) and a NavigationLink can both handle the task of navigation. The solution is to abandon one of them. You can use either to handle navigation.
Since List look good, I suggest sticking with that. The NavigationLink can then be removed. The second view under NavigationView is displayed on the right, so why not use DetailView(selected:) there. You already made the selected parameter a binding variable, so the view will update if that var changes.
struct ContentView: View {
#EnvironmentObject var modelData: ModelData
#State private var selected: URL?
var body: some View {
NavigationView {
List(selection: $selected) {
Section(header: Text("Bookmarks")) {
ForEach(modelData.myLinks, id: \.self) { url in
Text(url.absoluteString)
.tag(url)
}
}
}
.onDeleteCommand {
if let selected = selected {
modelData.myLinks.remove(at: modelData.myLinks.firstIndex(of: selected)!)
}
selected = nil
}
DetailView(selected: $selected)
}
}
}
I can recreate this problem with the simplest example I can think of so my guess is it's an internal bug in NavigationView.
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink("A", destination: Text("A"))
NavigationLink("B", destination: Text("B"))
NavigationLink("C", destination: Text("C"))
}
}
}
}

Passing data from extension in SwiftUI

I am building a complex interface in SwiftUI that I need to break into multiple extensions in order to be able to compile the code, but I can't figure out how to pass data between the extension and the body structure.
I made a simple code to explain it :
class Search: ObservableObject {
#Published var angle: Int = 10
}
struct ContentView: View {
#ObservedObject static var search = Search()
var body: some View {
VStack {
Text("\(ContentView.self.search.angle)")
aTest()
}
}
}
extension ContentView {
struct aTest: View {
var body: some View {
ZStack {
Button(action: { ContentView.search.angle = 11}) { Text("Button")}
}
}
}
}
When I press the button the text does not update, which is my issue. I really appreciate any help you can provide.
You can try the following:
struct ContentView: View {
#ObservedObject var search = Search()
var body: some View {
VStack {
Text("\(ContentView.self.search.angle)")
aTest // call as a computed property
}
}
}
extension ContentView {
var aTest: some View { // not a separate `struct` anymore
ZStack {
Button(action: { self.search.angle = 11 }) { Text("Button")}
}
}
}

Why doesn't calling method of child view from parent view update the child view?

I'm trying to call a method of a child view which includes clearing some of its fields. When the method is called from a parent view, nothing happens. However, calling the method from the child view will clear its field. Here is some example code:
struct ChildView: View {
#State var response = ""
var body: some View {
TextField("", text: $response)
}
func clear() {
self.response = ""
}
}
struct ParentView: View {
private var child = ChildView()
var body: some View {
HStack {
self.child
Button(action: {
self.child.clear()
}) {
Text("Clear")
}
}
}
}
Can someone tell me why this happens and how to fix it/work around it? I can't directly access the child view's response because there are too many fields in my actual code and that would clutter it up too much.
SwiftUI view is not a reference-type, you cannot create it once, store in var, and then access it - SwiftUI view is a struct, value type, so storing it like did you work with copies it values, ie
struct ParentView: View {
private var child = ChildView() // << original value
var body: some View {
HStack {
self.child // created copy 1
Button(action: {
self.child.clear() // created copy 2
}) {
Here is a correct SwiftUI approach to construct parent/child view - everything about child view should be inside child view or injected in it via init arguments:
struct ChildView: View {
#State private var response = ""
var body: some View {
HStack {
TextField("", text: $response)
Button(action: {
self.clear()
}) {
Text("Clear")
}
}
}
func clear() {
self.response = ""
}
}
struct ParentView: View {
var body: some View {
ChildView()
}
}
Try using #Binding instead of #State. Bindings are a way of communicating state changes down to children.
Think of it this way: #State variables are used for View specific state. They are usually made private for this reason. If you need to communicate anything down, then #Binding is the way to do it.
struct ChildView: View {
#Binding var response: String
var body: some View {
TextField("", text: $response)
}
}
struct ParentView: View {
#State private var response = ""
var body: some View {
HStack {
ChildView(response: $response)
Button(action: {
self.clear()
}) {
Text("Clear")
}
}
}
private func clear() {
self.response = ""
}
}

SwiftUI ObservedObject makes view pop on update

Why is my view going back to the parent when the ObservedObject updates? I understand that the update to it redraws the view (and presumably also the ParentView) but frankly atm that I am looking at the ChildView I don't care about the look of the ParentView.
I would understand the behaviour if the displayed stuff was coming directly from the ParentView but it is tied to a class. Is this intended behaviour from Apple?
How am I supposed to manipulate Data in a View that got called by a NavigationLink without popping the view?
Why is this pop forced?
Is there a way to "hold it back"?
I've tried something like this:
SwiftUI: #ObservedObject redraws every view but it didn't help. It just made it worse as I tap remove items anymore.
I have a code construct like this:
Parent View:
struct ContentView: View {
#ObservedObject var data = Data()
var body: some View {
NavigationView {
VStack() {
ForEach(self.data.myArray, id: \.self) { subArray in
NavigationLink(destination: Child(data: self.data, mySubArray: subArray)) {
Text(subArray[0]).padding().background(Color.yellow)
}
}
}
}
}
}
Child:
struct Child: View {
#ObservedObject var data: Data
var mySubArray: Array<String>
var body: some View {
ForEach(self.mySubArray, id: \.self) { str in
Text(str).padding().background(Color.yellow)
.onTapGesture {
self.data.removeFromSubArray(index: self.data.myArray.firstIndex(of: self.mySubArray) ?? 0, remove: str)
}
}
}
}
And an ObservableObject:
class Data: ObservableObject {
#Published var myArray: Array<Array<String>> = []
init() {
self.myArray.append(["A", "B", "C"])
self.myArray.append(["D", "E", "F"])
self.myArray.append(["G", "H", "I"])
}
func removeFromSubArray(index: Int, remove: String) {
if self.myArray.count-1 >= index {
if let subIndex = self.myArray[index].firstIndex(of: remove) {
self.myArray[index].remove(at: subIndex)
}
}
}
}