Post

Replies

Boosts

Views

Activity

Reply to SwiftUI Alert in Previews is not shown
This is the full code I used on macos 11.4, xcode 12.5, target ios 14.5 and macCatalyst. Is this not working for you? import SwiftUI @main struct TestApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { var body: some View { Alerts() } } struct Alerts: View { var body: some View { NavigationView { Text("Where is the Alert?") .alert(isPresented: .constant(true)) { Alert(title: Text("Hello"), message: Text("World")) } } } } struct Alerts_Previews: PreviewProvider { static var previews: some View { Alerts() } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’21
Reply to Not append new data in an array
what you are doing is resetting the "cart" to empty each time you call "Shop". Note, passing variables and etc... to another View is an important concept to master. Try this: struct ContentView: View { @ObservedObject var cart = Products() var body: some View { NavigationView { List { ForEach(cart.products) { product in Text("\(product.name) \(product.price)$") } } .navigationBarItems( trailing: NavigationLink(destination: Shop(cart: cart)) { // Text("Go Shop") }) .navigationBarTitle("Cart") }.navigationViewStyle(StackNavigationViewStyle()) } } struct Shop: View { @ObservedObject var cart:Products // var body: some View { VStack{ Button("Add Product To Cart") { cart.addProduct(product: Product(name: "Name", price: 399)) } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’21
Reply to How to reverse the bounds parameter of a slider
You cannot "reverse" the bounds. You will have to use some very basic math to calculate the desired value. According to the docs, Slider takes a ClosedRange for the "in" parameter, and a ClosedRange is (https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html): "The closed range operator (a...b) defines a range that runs from a to b, and includes the values a and b. The value of a must not be greater than b."
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’21
Reply to Anyone have a pointer about a crash Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000010
just an observation, I would avoid the "!" in: selectedPenaltyLength = formatSeconds(gameState.penaltyLengths![0]) and use: init(gameState : GameState) { self.gameState = gameState selectedPenaltyLength = "" selectedPenaltyIndex = 0 separatePenaltyClock = false if let penalties = gameState.penaltyLengths, penalties.count 0 { selectedPenaltyLength = formatSeconds(penalties[0]) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’21
Reply to How to delete in CoreData in an MacOS SwiftUI application (.onDelete is not working)
The ".onDelete(...)" works on MacOS, iOS and MacCatalyst. Here is a very basic example, using MacOS 11.4, xcode 12.5, target ios 14.5, macCatalyst 11.3 and MacOS 11.4. You have to swipe your mouse right to left. import SwiftUI @main struct TestApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { @State var items = ["1","2","3"] private func deleteItems(offsets: IndexSet) { withAnimation { print("deleteItems") } } private func addItem() { withAnimation { print("addItem") } } var body: some View { List { ForEach(items, id: \.self) { item in Text("Item at \(item)") }.onDelete(perform: deleteItems) } .toolbar { Button(action: addItem) { Label("Add Item", systemImage: "plus") } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’21
Reply to Dotted Line Border
try this: struct ContentView: View { var body: some View { Rectangle() .stroke(style: StrokeStyle(lineWidth: 2, dash: [5])) .foregroundColor(.red) .frame(width: 111, height: 111) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to Can not ForEach Int Array in Dictionary
could you try this: struct ContentView: View { @State private var notedNumbers: [Int:[Int]] = [0:[1,2]] var body: some View { ForEach(Array(notedNumbers.keys.enumerated()), id: \.element) { _, key in let row = key / 9 let col = key % 9 if let notedNumbersKey = notedNumbers[key] { ForEach(notedNumbersKey.indices, id: \.self) { i in // Cannot ForEach the Int Array here, compiler cannot type check the expression. Text("---> \(i)") } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21