Post

Replies

Boosts

Views

Activity

SwiftUI NavigationLink destination
In truth, I do not fully understand JSON Files and I am new to Swift. I am trying to be a bit too clever and build my menu from a JSON file. [ { "description": "Readings Entry", "target": "ReadingsEntry", "icon": "barometer", }, { "description": "Readings Entry", "target": "ReadingsView()", "icon": "tablecells.badge.ellipsis", }, { "description": "Food Diary", "target": "FoodDiary()", "icon": "fork.knife.circle", }, { "description": "Readings Entry", "target": "Settings()", "icon": "gearshape", } ] Because I don't know any better I have stored the destination view (target) as a String in the "struct" that I have constructed to import the JSON data. import SwiftUI struct dcMenu: Codable, Identifiable { enum CodingKeys: CodingKey { case description case target case icon } var id = UUID() var description: String var target: String var icon: String } class ReadData: ObservableObject { @Published var lineText = [dcMenu]() init(){ loadData() } func loadData() { guard let url = Bundle.main.url(forResource: "menu", withExtension: "json") else { print("Json file not found") return } let data = try? Data(contentsOf: url) let lineText = try? JSONDecoder().decode([dcMenu].self, from: data!) self.lineText = lineText! } } But the issue this leaves me with is when I come to construct the actual menu, How do I use the string "target" as the "destination" of the navigation link? struct ContentView: View { @ObservedObject var menuLines = ReadData() var body: some View { NavigationSplitView { . . . List(menuLines.lineText) { lines in NavigationLink(destination: lines.target){ Image(systemName: lines.icon) Text(lines.description) } } . . . .navigationSplitViewColumnWidth(min: 75, ideal: 100) } detail: { . . . } .navigationTitle("Diabetes Control") } }
2
0
396
Jun ’24
SwiftUI List and ForEach
What have I done wrong? I am trying to "List" a set of string values from an array, I spent all day trying to get it to work. What have I done wrong? import SwiftUI struct ContentView: View { @State private var menuItems = ["Settings", "Readings Entry", "Food Diary"] @State private var targets = ["Settings()", "ReadingsEntry()", "FoodDiary()"] var body: some View { NavigationStack { List { ForEach(menuItems) { menuLine in print(menuLine) } }.navigationTitle("Menu") } } } The latest set of Error messages are: Against the ForEach Line : Cannot convert value of type '[String]' to expected argument type 'Range' Against the print line3: Type '()' cannot conform to 'View'
2
0
775
Jun ’24
SwiftUI Picker Label
I have set up a "Picker" which works well, EXCEPT that the Label won't appear, I have struggled around this and I am sure the solution is very simple, here's what I have. import SwiftUI import SwiftData struct iPadReadingsEntry: View { @State private var whatOccasion: String = "" let occassions = ["Misc", "Breakfast", "Mid Day Meal", "Evening Meal", "Bed Time"] var body: some View { . . . HStack{ Picker("Before What Occasion :", selection: $whatOccasion ){ ForEach(occassions, id: \.self) { Text($0) } }.pickerStyle(MenuPickerStyle()) .background(Color(red: 255, green: 253, blue: 208)) Spacer() }.padding(.leading, 80) .padding(.bottom, 30) . . .
1
0
653
May ’24
Swift: Navigation link not working.
I have obviously missed some subtle bit of code but I can't see what I am missing. I am getting a warning about my NavigationLink. Result of 'NavigationLink<Label, Destination>' initializer is unused Below I have included the code that has caused the warning and the code of the target of the Navigation link. The link does not work, though the button does, as proved by the print statement (see below). Here is the code that caused the warning: import SwiftUI import SwiftData struct Main_Menu_iPad: View { @Environment(\.modelContext) private var context @Query private var readings: [Readings] var body: some View { ZStack { Image("iPad Background 810 X 1060") .resizable() .scaledToFill() .ignoresSafeArea() VStack { HStack{ Image("Diabetes Control Logo 1024X1024") .resizable() .frame(width: 100, height: 100, alignment: .leading) .padding(.leading, 40) Text("Diabetes Control") .font(Font.custom("SnellRoundhand-Black", size: 40)) .background(Color(red: 255, green: 253, blue: 208)) .shadow(color: Color(red: 128, green: 128, blue: 128), radius: 3) .padding(.leading, 150) Spacer() }.padding(.bottom, 35) HStack { Text("What would you like to do : ") .font(Font.custom("SnellRoundhand-Black", size: 30)) .background(Color(red: 128, green: 128, blue: 128)) .shadow(color: Color(red: 126, green: 128, blue: 128), radius: 3) Spacer() }.padding(.leading, 35) .padding(.bottom,35) NavigationStack { HStack { Button { print("I have been pressed") NavigationLink { iPadReadingsEntry() } label: { Text("Enter a BLOOD Glucose reading") } } label: { Text("Enter a Blood Glucose Reading") }.background(Color(red: 255, green: 253, blue: 208)) .foregroundColor(.black) .font(Font.custom("SnellRoundhand", size: 24)) Spacer() }.padding(.leading, 60) .padding(.bottom, 25) Spacer() }.background(.blue) Spacer() } } } } #Preview { Main_Menu_iPad() } And this is the code of the target view: import SwiftUI struct iPadReadingsEntry: View { var body: some View { ZStack { Image("iPad Background 810 X 1060") .resizable() .scaledToFill() .ignoresSafeArea() VStack { HStack{ Image("Diabetes Control Logo 1024X1024") .resizable() .frame(width: 100, height: 100, alignment: .leading) .padding(.leading, 40) Text("Diabetes Control") .font(Font.custom("SnellRoundhand-Black", size: 40)) .background(Color(red: 255, green: 253, blue: 208)) .shadow(color: Color(red: 128, green: 128, blue: 128), radius: 3) .padding(.leading, 150) Spacer() }.padding(.bottom, 35) Spacer() } } } } #Preview { iPadReadingsEntry() } Finally is there a way of switching View without using a NavigationStack/NamigastinLink as it covers my background image?
1
0
515
May ’24
SwiftUI Trap change of orientation
The real truth is that I don't know what I am doing. I am trying to trap the change of orientation event. I have been strongly advised to use "Size Classes" rather than "Landscape" or "Portrait". Apparently, this is what Apple recommends. I have tried two or three ways to do this, and my closest effort to get it working has run me into an Issue (code below) that I don't know how to solve. I believe I have to use the @Evinroment macros to pick up the current size classes (@Environment(\.horizontalSizeClas) var .... and @Environment(\.verticalSizeClass) var ....) and these have to go inside a "View", I think. But I know when I get to setting up the Notification Center I will have to use the @Published macro to pass the orientation value back to my main "ContentView", again I think. And the @Published macro needs to run in a class. So I have tried to put a "View" inside a "class" and run into an issue that the @Pulished variable (a String) is only recognised in the "class" and the size classes are only recognised in the "View". So How do I overcome this? Here is the Code I have come up with so far, it is incomplete and when I get this working I will add more. import SwiftUI final class OrientChange { @Published public var myOrient: String init(myOrient: String){ self.myOrient = myOrient } struct SizeClassView: View { @Environment(\.horizontalSizeClass) var myHorizClass: UserInterfaceSizeClass? @Environment(\.verticalSizeClass) var myVertClass: UserInterfaceSizeClass? var body: some View { Text("Horiz Class: \(myHorizClass)") if myHorizClass == .compact && myVertClass == .regular { OrientChange.myOrient = "Portrait" } else { Text("No") } } } } #Preview { OrientChange.SizeClassView() }
2
0
526
May ’24
SwiftUI Orientation Change
I have been struggling for nearly a week to handle orientation changes. In a previous post https://developer.apple.com/forums/thread/755957 I was strongly advised to use Size Classes, and I am trying to do that. by following this post: https://forums.developer.apple.com/forums/thread/126878) But I still can get it to work, so far I am just trying to initialize all the variables I will use later on. please bear with me I am 65 and have not done any coding for coming for 40 years. This my latest effort: import SwiftUI final class myOrientationChange: ObservableObject { @Published var myOrient: String = "" @Environment(\.verticalSizeClass) var myVertClass: UserInterfaceSizeClass? @Environment(\.horizontalSizeClass) var myHorizClass: UserInterfaceSizeClass? var _observer: NSObjectProtocol? if myHorizClass == .compact && myVertClass == .regular { self.myOrient = "Portrait" } elseif myHorizClass == .regular && myVertClass == .compact { self.myOrient = "Landscape" } else { self.myOrient = "Something Else" } } struct ContentView: View { @EnvironmentObject var envOrient: myOrientationChange var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") Text("Orientation is \(envOrient.myOrient)") } .padding() } } #Preview { ContentView() } I will go on to use the NotificationCenter to trap there change of orientation even. On the line if myHorizClass == ......... It tells me "Expected declaration in declaration of 'myOrientChange'" What have I done wrong?
1
0
594
May ’24
SwiftUI: How do I tell when my device orientation changes
Sorry to repeat this, but the new style forum won't allow me access to the original version of this question, and the answers. I have been searching the internet for 3 or 4 days now to find only complex solutions that I cannot get working. All I want to do is determine when the device orientation changes so that I can update the background Image. This is what I have so far: Import SwiftUI struct Main_Menu_iPhone: View { @State private var bloodGlucose: Float = 0.0 var body: some View { ZStack { Image("iPhone Background Portrait 828 x 1792 ") .resizable() .aspectRatio(contentMode: .fill) VStack { HStack { Label("Blood Glucose", systemImage: "drop.fill") TextField("Blood Glucose : ", value: $bloodGlucose, format: .number) .onSubmit() { print("Your Blood Gluse is : \(bloodGlucose)") } .background(.white) .frame(alignment: .topTrailing) Spacer() } Spacer() }.padding(.top, 250) } } }
1
0
495
May ’24
SwiftUI: How do I detect a change in device orientation?
Please can someone help I have spent 3 - 4 days trawling the internet and only finding complex answers that I can't get to work, and my head is ready to explode. I only want to detect when the device orientation has changed and load a different background image. This is what I have so far: import SwiftUI struct Main_Menu_iPhone: View { @State private var bloodGlucose: String = "0.0" var body: some View { ZStack { Image("iPhone Background Portrait 828 x 1792 ") .resizable() .aspectRatio(contentMode: .fill) VStack { TextField("Blood Glucose : ", text: $bloodGlucose) Spacer() } } } } #Preview { Main_Menu_iPhone() }
2
0
820
May ’24
Swift: Can I change views without clicking on a link.
I want to automatically load different views depending on OS (OSX or iOS). Is there a way that I can do this without the user having to click on a link? This is my code so far. struct ContentView: View { #if os(iOS) var myOS = "iOS" #elseif os(OSX) var myOS = "OSX" #else var myOS = "Something Else" #endif var body: some View { NavigationStack { VStack { Text("PLEASE WAIT....") .font(.system(size: 24)) .fontWeight(.bold) } .padding() if (myOS == "OSX"){ // Goto Screen for iMac } else{ // go to screen for iOS } } } } If I use "NavigationLink", my understanding is that the user would need to click on a link. Is there some way to do this without user interaction?
2
0
364
May ’24
Why can't I get text to appear at top of view.
No matter what I have tried, I can't get my test "What would you like to do?" to appear at the top of the screen. What have I missed? Is something g to do with the ZStack? import SwiftUI import SwiftData struct ContentView: View { @Environment(\.modelContext) private var context @Query private var readings: [Readings] @State private var readingTimeStamp: Date = Date() var body: some View { ZStack { Image("IPhone baqckgound 3") .resizable() .aspectRatio(contentMode: .fill) .padding(.top, 40) VStack { Text("What would you like to do?") .font(.title) .fontWeight(.bold) } .padding() Spacer() } } }
3
0
398
May ’24
SwiftData relationships
I am new to Swift, and I am trying to get a grip on SwiftData, which SO FAR seems pretty straightforward EXCEPT relationships between tables. I come from a MYSQL, SQLITE3 background and I am used to “Primary Keys”, Inner and Outer Joins, One-to-One and One-to-Many relationships. Which SwiftData does not appear to do (I think). I am following several reasonably good online tutorials on the subject, all of which gloss over how relationships work. The best one is a table for vegetables alongside a table of notes for each vegetable (A one-to-many relationship). The classes we set up for the schemas are as follows. import Foundation import SwiftData @Model class Vegetable { var name: String @Relationship(deleteRule: .cascade) var notes: [Note]? init(name: String) { self.name = name } } @Model class Note { var text: String var vegetable: Vegetable? init(text: String) { self.text = text } } I can’t comprehend how they work. None of the tutorials I have watched explain it or explain it in a way I can understand. In the “Vegetable” class, why is the field “notes” an array of type Notes? Again in the “Vegetable” class does the field “notes” get stored in the database, if so what is stored? In the “Note” Class it looks like the whole of the class “Vegetable” gets stored in the variable “vegetable”, which may or may not get stored in the database.
1
0
739
May ’24
Swift UI How can I get the click of a button to change the wording of Label
Using SwiftUI how can I get the click of a "Button" to change the wording of a "Label" and/or "Text" Field? I have tried:- let myLabel = Label { Text("Text to be Changed" .foregrorundStyle ...... } icon: { ...... } .... ..... Button("Change Label Wording"){ myLabel.stringValue = "Changed text" } This gives me two problems (at least): I cannot get the label to display The myLabel.stringValue = "Changed text gives me the error: Type '()' cannot conform to 'View' Value of type 'Label<some View, some View>' has no member 'stringValue' What have I done wrong?
1
0
376
May ’24