Post

Replies

Boosts

Views

Activity

Toolbars in Sheets merge with Container View
I'm using SwiftUI on macOS. The idea is to create a sheet, which has two buttons. One for cancellation and one for done actions. They are placed with their .confirmationAction and .confirmationAction placement options inside ToolbarItems. When I now open the sheet, the container also adds this buttons to it's toolbar. Is there something wrong with my code or is this considered an issue with SwiftUI? Thank you for your assistance. Tobias // //  ContentView.swift //  Toolbar // //  Created by Tobias on 16.12.20. // import SwiftUI struct ContentView: View {     enum Selection: String {         case First     }     @State private var selection: Selection? = .First     var body: some View {         NavigationView {             List {                 NavigationLink(                     destination: DetailsView(),                     tag: Selection.First,                     selection: $selection                 ) {                     Text("First")                 }             }             .listStyle(SidebarListStyle())         }     } } struct DetailsView: View {     @State private var displayAdd = false     @State private var message = "Initial Message"     var body: some View {         Text(message)             .toolbar {                 ToolbarItem(placement: .primaryAction) {                     Button("Add") {                         self.displayAdd = true                     }                 }             }             .sheet(isPresented: $displayAdd) {                 SheetView(completion: didFinish)             }     }     func didFinish(_ result: SheetView.Result) {         displayAdd = false         switch result {             case .Success:                 message = "Success"             default:                 message = "Cancelled"         }     } } struct SheetView: View {     typealias Completion = (Result) -> Void     enum Result {         case Cancel         case Success     }     private var completion: Completion     init(completion: @escaping Completion) {         self.completion = completion     }     var body: some View {         Text("Modal Sheet")             .toolbar {                 ToolbarItem(placement: .confirmationAction) {                     Button("Done") {                         completion(.Success)                     }                 }                 ToolbarItem(placement: .destructiveAction) {                     Button("Cancel") {                         completion(.Cancel)                     }                 }             }     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } } Reproducible SwiftUI code. - https://developer.apple.com/forums/content/attachment/92a31ff1-caa0-4ee3-86fd-86d4c5413592
0
0
655
Dec ’20
Value from @State variable does not change
I have created a View that provides a convinient save button and a save method. Both can then be used inside a parent view.The idea is to provide these so that the navigation bar items can be customized, but keep the original implementation.Inside the view there is one Textfield which is bound to a @State variable. If the save method is called from within the same view everthing works as expected. If the parent view calls the save method on the child view, the changes to the @State variable are not applied.Is this a bug in SwiftUI, or am I am missing something? I've created a simple playbook implementation that demonstrates the issue.Thank you for your help.import SwiftUI import PlaygroundSupport struct ContentView: View { // Create the child view to make the save button available inside this view var child = Child() var body: some View { NavigationView { NavigationLink( destination: child.navigationBarItems( // Set the trailing button to the one from the child view. // This is required as this view might be inside a modal // sheet, and we need to add the cancel button as a leading // button: // leading: self.cancelButton trailing: child.saveButton ) ) { Text("Open") } } } } struct Child: View { // Store the value from the textfield @State private var value = "default" // Make this button available inside this view, and inside the parent view. // This makes sure the visibility of this button is always the same. var saveButton: some View { Button(action: save) { Text("Save") } } var body: some View { VStack { // Simple textfield to allow a string to change. TextField("Value", text: $value) // Just for the playground to change the value easily. // Usually it would be chnaged through the keyboard input. Button(action: { self.value = "new value" }) { Text("Update") } } } func save() { // This always displays the default value of the state variable. // Even after the Update button was used and the value did change inside // the textfield. print("\(value)") } } PlaygroundPage.current.setLiveView(ContentView())
3
0
3.0k
Feb ’20