Post

Replies

Boosts

Views

Created

Mixing NavigationLink types (value: and non-value: types)
Hello, I was wondering if someone could clear-up my thinking here. e.g. consider the code below... It has a rootView with a navlink to a childView which in turn has navlinks to GrandchildViews. The root view uses basic navLInks NavigationLink{View} label: {View} The child view uses type-based navLinks navigationLink(value:) {View} and .navigationDestination(for:) {View} I would expect the basic navlinks to work in the root view and the type-based ones to work in the child view. However it appears that both are active when one taps on a link in the child view. e.g. User actions: Start -> RootView is only view on the stack -> (tap on ‘Child View’) -> ChildView is top of the stack -> tap on ‘Alice’ -> a second ChildView is top of the stack with a GrandchildView underneath…. Why does this happen, why are the basic links also applied to the childView's links? Thanks. struct Thing: Identifiable, Hashable { let id = UUID() let name: String } struct RootView: View { var body: some View { NavigationStack { List { NavigationLink { ChildView() } label: { Label("Child View", systemImage: "figure.and.child.holdinghands") } NavigationLink { Text("Hello") } label: { Label("Another navLink item in the list", systemImage: "circle") } } .padding() } } } struct ChildView: View { private var things = [ Thing(name: "Alice"), Thing(name: "Bob"), Thing(name: "Charlie"), ] var body: some View { Text("This is the child view") List { ForEach(things) { thing in NavigationLink(value: thing) { Text(thing.name) } } } .navigationTitle("Child View") .navigationDestination(for: Thing.self) { thing in GrandchildView(thing: thing) } } } struct GrandchildView: View { let thing: Thing var body: some View { Text("This is the GrandchildView: \(thing.name)") } }
Topic: UI Frameworks SubTopic: SwiftUI
0
0
123
Mar ’25
TextField("x", value: $y, format: .currency(code: "USD")) Behaves oddly
Hello, If we have the following code: import SwiftUI struct ContentView: View { @State private var usdAmount = 0.0 var body: some View { VStack { Text("Currency Form") Form { TextField("Dollar Amount", value: $usdAmount, format: .currency(code: "USD")) // 1 // TextField("Dollar Amount", value: $usdAmount, format: .number) // 2 .keyboardType(.numberPad) } Text("usdAmount = \(usdAmount)") } .padding() } } When the line marked // 1 is left uncommented (the TF has a .currency() formatter applied) the behaviour of the TF is rather odd. Upon display it shows US$0.00 Upon editing the value the US$ part remains. So if the user enters US$123 the value is not stored into the usdAmount property. The User must delete all the existing text and enter e.g. 456.78 and then the value is stored to the usdAmount property. When Line //1 is commented-out and line //2 is uncommented then the TF behaves correctly because the TF value is just 0 on first display. Should we not use the .currency() format? Or am I using it wrong? Thanks.
Topic: UI Frameworks SubTopic: SwiftUI
2
0
802
Jun ’24
Returning a type from an Intent (basics)
Hello, I'm attempting to learn the basics of AppIntents. My test Hello World intent takes a number and doubles it. This page (https://developer.apple.com/documentation/appintents/providing-your-app-s-capabilities-to-system-services) seems to imply that you can return types from the perform() function. My code compiles, but crashes at runtime with the error perform() returned types not declared in method signature - Did not declare ReturnsValue but provided one Code: struct DoubleANumber: AppIntent { static var title: LocalizedStringResource = "Double a number" static var description = IntentDescription("Given a number, gives back twice that number.") @Parameter(title: "Start Number") var inputNumber: Double static var parameterSummary: some ParameterSummary { Summary("The number to double") } func perform() async throws -> some IntentResult & ReturnsValue { let outputNumber = inputNumber * 2 return .result(value: outputNumber) } } The value returned in the value property of the IntentResult is a Double. I would have assumed that this would be a valid primitive type (as mentioned in the earlier section of that docs page, covering parameters) and that the actual type returned is the wrapper .result type would be covered in the type in the method signature some IntentResult & ReturnsValue What am I missing? Thanks.
2
0
1.3k
Nov ’23
macOS cannot use custom UNNotificationSounds ?
Hello, The documentation states that one can add a custom sound to User Notifications. This works on iOS, but doesn't appear to work on macOS. Am I holding it wrong, or are the docs wrong? https://developer.apple.com/documentation/usernotifications/unnotificationsound Example minimal project to demo is here: https://github.com/Diggory/macOSLocalNotifications Thanks.
4
0
899
Oct ’22
Cannot scan for iBeacons (macOS)
Hello, I am attempting a very simple AppKit app on MacOS which displays nearby iBeacons. I don't appear to ever have region scanning available on my system though. Am I doing something wrong? FYI: My system is a MacBook Pro (16-inch, 2019) running Catalina (10.15.7) The target has 'Bluetooth' and 'App Location' enabled in the 'App Sandbox' capabilities section and 'Location' enabled under 'Hardened Runtime'. For a second question: is it possible to scan for any beacons, not just my own (i.e. scan without setting a proximity UUID)? //  ViewController.swift //  Beacons import Cocoa import CoreLocation class ViewController: NSViewController, CLLocationManagerDelegate {     var locationManager: CLLocationManager!     override func viewDidLoad() {         super.viewDidLoad()         locationManager = CLLocationManager()         locationManager.requestAlwaysAuthorization()         locationManager.delegate = self     }     @IBAction func buttonTapped(_ sender: NSButton) {         self.startScanning()     }     override var representedObject: Any? {         didSet {             // Update the view, if already loaded.         }     }     func startScanning() {         print("starting scanning") //UUID Obscured for forum post...         guard let uuid = UUID(uuidString: "xxxxxxxx-E4A1-4720-9034-xxxxxxxxxxxx") else {             print("Couldn't make UUID")             return         }         let beaconID = "com.monkeyfood.myBeaconRegion"         let beaconRegion = CLBeaconRegion(uuid: uuid, identifier: beaconID) //        let beaconRegion = CLBeaconRegion(uuid: UUID(), identifier: beaconID)         self.locationManager.startMonitoring(for: beaconRegion)     }          //. CLLocationManagerDelegate Methods     func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {         var newStatus = ""         switch status {         case .notDetermined:             newStatus = "Not determined"         case .restricted:             newStatus = "Restricted"         case .denied:             newStatus = "Denied"         case .authorizedAlways:             newStatus = "Authorised Always"         case .authorized:             newStatus = "Authorised"         default:             newStatus = "What?"         }         print("LM Auth Status is now: \(newStatus)") // Check for iBeacon monitoring status         let regionMonitoringIsAvailable = CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self)         let not = regionMonitoringIsAvailable ? "" : "not "         print("Beacon monitoring is \(not)available")     }     func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {         if beacons.count > 0 { //            updateDistance(beacons[0].proximity)             print("\(beacons.count) beacons found.")             print("\(beacons[0].proximity) dist.")         } else { //            updateDistance(.unknown)         }     }     func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {         print("region entered... \(region)")     }     func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {         print("region exited... \(region)")     }     func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {         print("determined  region state")     }     func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {         print("region monitoring failed: \(error)")     }     func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {         print("LM failed \(error)")     } } Always gives me the following: LM Auth Status is now: Authorised Always Beacon monitoring is not available starting scanning region monitoring failed: Error Domain=kCLErrorDomain Code=5 "(null)"
1
0
1.4k
Jul ’21