Post

Replies

Boosts

Views

Activity

Strange behaviour with NavigationView and Buttons
I'm getting really strange results when pushing views onto a NavigationView stack and using buttons. This happens in the latest Xcode beta. Sample code: struct TestView: View {     var body: some View {         NavigationView {             VStack {                 NavigationLink("To view 2", destination: View2()) // Doesn't work                 NavigationLink("To view 3", destination: View3()) // Works             }             .navigationTitle("View 1")             .navigationBarTitleDisplayMode(.large)         }     } } struct View2: View {     var body: some View {         VStack {             NavigationLink("To view 3", destination: View3())         }         .navigationTitle("View 2")         .navigationBarTitleDisplayMode(.large) // Setting to .inline makes it work     } } struct View3: View {     var body: some View {         VStack {             Button("Button 1") { print("Button 1") }             Button("Button 2") { print("Button 2") }             Button("Button 3") { print("Button 3") }             Button("Button 4") { print("Button 4") }             Button("Button 5") { print("Button 5") }             Spacer() // Removing this makes it work         }         .navigationTitle("View 3")         .navigationBarTitleDisplayMode(.inline)     } } There are three views. View1 has links to View2 and View3. View2 has a link to View3. View3 has a number of buttons. If I go View1 -> View3, everything works. If I go View1 -> View2 -> View3, the first 2-3 buttons don't work. If I remove the Spacer() from View3, it works. If I change the navigationBarTitleDisplayMode of View2 to .inline, it works. This looks like a bug to me. Are there any workarounds?
0
0
297
Sep ’21
Control IP version of URLSession
When connecting to a URL using URLSession, is it possible to somehow control if URLSession should pick an IPv4 or IPv6 address? I have a URL and I need to make two connections to the URL, one using IPv4 and one using IPv6. The DNS name used in the URL resolves to both IPv4 and IPv6 addresses and I can't change this. Is there any way to affect which IP version (or IP number in general) that URLSession picks when resolving the name?
3
1
731
Aug ’20
Popped navigation view is still being updated
I'm having a problem with old views that have been popped from the navigation stack still seems to be "updated" in the background even though they are not visible or on the navigation stack. I do the following: The main view is pushing a sub view on the navigation stack. The sub view becomes visible. The user uses the back arrow to go back, popping the sub view from the navigation stack. The main view is now visible. The main view updates and this seems to cause the sub view to also update, even though it shouldn't be active. The problem I have is that when the data model is being updated the sub view crashes since it's trying to access data that doesn't exist anymore. Sample code: import SwiftUI struct ContentView: View {     var body: some View {         TestView(items: [1, 2, 3, 4, 5])     } } struct TestView: View {     @State var items: [Int]     var body: some View {         NavigationView {             VStack {                 Text("Hello")                 List {                     ForEach(items, id: \.self) { item in                         NavigationLink(destination: SubView(items: self.$items, number: item)) {                             Text("\(item)")                         }                     }                 }                 Button(action: {                     self.items.append(self.items.count + 1)                 }) {                     Text("Add")                 }             }         }     } } struct SubView: View {     @Binding var items: [Int]     var number: Int     var body: some View {         Group {             Print("\(number)")             Text("\(number)")         }     } } extension View {     func Print(_ items: Any..., separator: String = " ", terminator: String = "\n") -> some View {         let output = items.map { "\($0)" }.joined(separator: separator)         Swift.print("→ " + output, terminator: terminator)         return EmptyView()     } } In the sample, clicking a row in the list simply uses SubView to show the number and also log it to the console. If I return to the main view and click the Add button to force an update of the binding variable, the log will show that the SubView is being updated again. It seems that I can fix it using: struct SubView: View { 		@Binding var items: [Int] 		var number: Int 		@State var active = true 		 		var body: some View { 				Group { 						if active { 								Print("\(number)") 								Text("\(number)") 						} 				} 				.onDisappear { self.active = false } 		} } But that just seems silly and I'm not sure it will work in all situations. Am I doing something wrong? Is this expected behaviour? Is there any way I can stop the SubView from updating after it has been dismissed?
3
0
2.4k
Jun ’20