swiftui

SwiftUI .onDelete throws Fatal Error: Index out of range

£可爱£侵袭症+ 提交于 2021-01-29 22:33:49
问题 I have list of Account s that each of them has a detail page and they are connected through @Binding in the AccountDetailView . Current code work well, updates are fine. No problem at all. However when I added the onDelete modifier to the ForEach below and tried swipe to delete gesture in app, it crashes and says Fatal Error: Index out of range twice. I made some search and learned that ForEach somehow does not get notified -or ignores it, idk much detail- and looks for the last index of the

How to get SwiftUI Picker in subview working? (Greyed out)

守給你的承諾、 提交于 2021-01-29 21:31:02
问题 I'm working on a SwiftUI project and am having trouble getting a picker to work correctly. I've got a hierarchy of views split into multiple files with the initial view wrapping everything in a NavigationView. Looks something like this: MainFile (TabView -> NavigationView) - ListPage (NavigationLink) -- DetailHostPage (Group.EditButton) if editing --- DetailViewPage else --- DetailEditPage (picker in a form) The picker that I have in the DetailEditPage does not let me change it's value,

SwiftUI UIDatePicker .compact doesn't go directly to pop-up

ぐ巨炮叔叔 提交于 2021-01-29 20:48:42
问题 I have created a custom DatePicker using UIDatePicker and when I select the field it displays the date and time as an option at the bottom of the screen without going directly to the pop-up. Not sure how to describe it so please see the image below: Please see my code below: import SwiftUI struct CustomDateTimePicker: View { @State private var date: Date? @State private var time: String? = "Time" var body: some View { HStack { Image("Time") .resizable() .aspectRatio(contentMode: .fit) .frame

SwiftUI hiding a navigation bar only when looking at ContentView

我是研究僧i 提交于 2021-01-29 20:19:34
问题 I have a Content file and am hiding the navigation bar because it takes up space and pushes elements down. One of the buttons in the ContentView redirects (using a navigation link) to another view. In this other view, the navigationBar is still hidden....for simplicity sake, I'll cut out some of the code from ContentView: //this is the view that looks "fine" (i.e. the navigation bar takes up no space) struct ContentView: View { @State private var isPresentedSettings = false var body: some

SwiftUI: @ObservedObject redraws every view

老子叫甜甜 提交于 2021-01-29 20:12:02
问题 I try to implement the MVVM way correctly in SwiftUI, so I came up with this (simplified) Model and ViewModel: struct Model { var property1: String var property2: String } class ViewModel: ObservableObject { @Published var model = Model(property1: "this is", property2: "a test") } Using this in a View works fine, but I experienced some bad performance issues, as I extended the ViewModel with some computed properties and some functions (as well the Model itself is more complicated). But let's

Decoding Exchange Rate JSON in SwiftUI

笑着哭i 提交于 2021-01-29 20:10:44
问题 I am trying to decode https://api.exchangeratesapi.io/latest, provided by Exchange Rates API. I'm applying several tutorials I found online, but when I apply my own details, I get an error. My code looks as following: struct Response: Codable { var results: [Result] } struct Result: Codable { let base: String let date: String let rates: [String:Double] } The function to retrieve the data: func loadData() { guard let url = URL(string: "https://api.exchangeratesapi.io/latest") else { print(

WatchKit WatchOS - Location Services Prompt never displayed on the Apple Watch

怎甘沉沦 提交于 2021-01-29 20:02:28
问题 When I call requestWhenInUseAuthorization() from an Watch App, the Location Services prompt is never displayed to the user: neither in the Watch app, nor in the iPhone app. The Support Running Without iOS App Installation is not checked (if I check it, the prompt is correctly displayed, but I don't want my app to work without an iOS companion app). I added the NSLocationWhenInUseUsageDescription key in the info.plist of both the iPhone app and the Watch Extension. I also tried to add the

Tapping an MKMapView in SwiftUI

耗尽温柔 提交于 2021-01-29 19:43:14
问题 I have a map in a SwiftUI app. It is working up to a point; but now I want to be able to tap on it and know the latitude and longitude of the tap. Here is the current code: import SwiftUI import MapKit struct MapView: UIViewRepresentable { @Binding var centerCoordinate: CLLocationCoordinate2D func makeUIView(context: Context) -> MKMapView { let mapView = MKMapView() mapView.delegate = context.coordinator let gRecognizer = UITapGestureRecognizer(target: context.coordinator, action: #selector

SwiftUI toggle() function on Published values stopped triggering didSet with Swift 5.2

筅森魡賤 提交于 2021-01-29 19:08:49
问题 I have just updated my Xcode to 11.4 from 11.3 and my project written in SwiftUI started to behave differently. I used to call toggle() function for boolean values and it used to trigger didSet property observer, however, it is not working any more. Let' say we have a State property called isSettingOn . I used to call this: isSettingOn.toggle() which was triggering didSet observer of the property. Now, only if I call this: isSettingOn = true it is working. My projects are all based on this

EnvironmentObject does not work when sheet is presented

老子叫甜甜 提交于 2021-01-29 18:52:50
问题 I have a view that has managedObjectContext(Environment Variable). It works fine when presented in NavigationView with NavigationLink. But it does not work when the view is presented as a sheet. It gives me an error when I save some entity data. "The operation couldn’t be completed. (Foundation._GenericObjCError error 0.)" Is there anything I am missing when using the Environment object. My code let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext