Post

Replies

Boosts

Views

Activity

Reply to Calculations using picker options in swiftUI
Thanks Claude31 for your prompt response and helping with error. Previously, I was trying with radioButtons as posted couple of weeks ago but that didn't work, so I tried using Picker, and again was stuck at conversions. Thanks again Now I cam work with other parameters to do further calculations.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’21
Reply to SwiftUI help with application of multiple radio buttons to perform calculations based on selected option
Thanks. The code was too long and wouldn't fit in one window, I deleted Race code to fit, and was similar in pattern. The RadioButtonGroups is first declared under Sex RadioButtonGroups, and for the remainder buttons, I added the Height, Weight, Race etc in front to assure each Button being unique. Yes, I used, the video presented at this link https://thinkdiff.net/how-to-create-radio-button-and-group-in-swiftui-46b34e0ba69a to make radio Buttons and changed the names to fit. Here the author did use "@State var selectedId: String = "" ", so I used the same. I have capitalized the first letter of struct where I missed. for each button have to access the property, so if I know how to access for one button then can use the same code for others. So how to access this "HeightRadioInchesMajority" for "Height" buttons. Yes there is a textfield where the value is entered, and then one of the two buttons ('"Cms" or "inches") is clicked by the person entering the value. IN order to do calculations, need to convert values to one unit (metric) and then perform calculations. The question is how to access this property, as I am at present trying to do and learn for one of the buttons, first. I have code in two swiftUI files at present, ContentView RadioButtons
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to SwiftUI help with application of multiple radio buttons to perform calculations based on selected option
Part 2, import SwiftUI struct ContentView: View {     @State var age = ""     @State var sex = ""     @State var race = ""     @State var height = ""     @State var weight = ""     @State var creatinine = ""          var body: some View {             VStack {                                  Text("")                 Spacer()                 Group{                             HStack {                         Text("Age (Years):")                             .fontWeight(/@START_MENU_TOKEN@/.bold/@END_MENU_TOKEN@/)                             .frame(width: 200, height: 40, alignment: .leading)                             .padding(.leading, 20)                                                  Spacer()                                                  TextField("Enter age:", text: $age)                             .frame(width: 100, height: 40, alignment: .leading)                             .multilineTextAlignment(.trailing)                             .padding(.trailing, 20)                     }                     HStack {                         Text("Height:")                             .fontWeight(/@START_MENU_TOKEN@/.bold/@END_MENU_TOKEN@/)                             .frame(width: 100, height: 40, alignment: .leading)                             .padding(.leading, 20)                         Spacer()                         heightRadioButtonGroups { selected in                             return                                                   }                                                  TextField("Enter Height:", text: $height)                             .frame(width: 80, height: 40, alignment: .leading)                             .multilineTextAlignment(.trailing)                             .padding(.trailing, 20)                     }                     HStack {                         Text("Weight:")                             .fontWeight(/@START_MENU_TOKEN@/.bold/@END_MENU_TOKEN@/)                             .frame(width: 100, height: 40, alignment: .leading)                             .padding(.leading, 20)                                                  Spacer()                                                  weightRadioButtonGroups { selected in                             return                                                    }                                                  TextField("Enter weight", text: $weight)                             .frame(width: 80, height: 40, alignment: .leading)                             .multilineTextAlignment(.trailing)                             .padding(.trailing, 20)                                                                       }                                      Spacer()                                  Button(action: {                                          //calculate()                                      }, label: {                     Text("CALCULATE")                         .font(.largeTitle)                         .fontWeight(.bold)                         .foregroundColor(Color.white)                         .background(Color("teal"))                         .frame(width: 200, height: 40, alignment: /@START_MENU_TOKEN@/.center/@END_MENU_TOKEN@/)                         .clipShape(RoundedRectangle(cornerRadius: 12))                         .shadow(radius: 14)                 })                                                      Spacer()                                          Button(action: {                                                  // reset()                                              }, label: {                         Text("RESET")                             .font(.largeTitle)                             .fontWeight(.bold)                             .foregroundColor(Color.white)                             .background(Color("teal"))                             .frame(width: 140, height: 40, alignment: /@START_MENU_TOKEN@/.center/@END_MENU_TOKEN@/)                             .clipShape(RoundedRectangle(cornerRadius: 12))                             .shadow(radius: 14)                     })                                      }                              }                      }              }              func calculate() -> Double {              var heightCms: Double = 0.0                  if (heightRadioButtonGroups(callback: heightRadioInchesMajority).isSelected) {             return Double(height) ?? 1 * 2.54         } else {             return Double(height) ?? 1         }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to SwiftUI help with application of multiple radio buttons to perform calculations based on selected option
Thanks, it was long code, here are the details in the view of all the radio buttons, here is the `// //  RadioButtons.swift //  De-Indexed GFR // //  Created by Loveleen Parmar on 2021-09-21. // import SwiftUI //MARK:- Single Radio Button Field struct RadioButtonField: View {     let id: String     let label: String     let size: CGFloat     let color: Color     let textSize: CGFloat     let isMarked:Bool     let callback: (String)->()          init(         id: String,         label:String,         size: CGFloat = 20,         color: Color = Color.black,         textSize: CGFloat = 14,         isMarked: Bool = false,         callback: @escaping (String)->()     ) {         self.id = id         self.label = label         self.size = size         self.color = color         self.textSize = textSize         self.isMarked = isMarked         self.callback = callback     }          var body: some View {         Button(action:{             self.callback(self.id)         }) {             HStack(alignment: .center, spacing: 10) {                 Image(systemName: self.isMarked ? "largecircle.fill.circle" : "circle")                     .renderingMode(.original)                     .resizable()                     .aspectRatio(contentMode: .fit)                     .frame(width: self.size, height: self.size)                                  Text(label)                     //.font(Font.system(size: textSize))                     .font(.caption)                                  Spacer()             }.foregroundColor(self.color)         }         .foregroundColor(Color.white)     } } //MARK:- Group of weight Radio Buttons enum Weight: String {     case kg = "Kgs"     case pounds = "lbs" } struct weightRadioButtonGroups: View {     let callback: (String) -> ()          @State var selectedId: String = ""          var body: some View {         HStack {             weightRadioKgMajority             weightRadioPoundsMajority         }.padding(.leading, 6)     }          var weightRadioKgMajority: some View {         RadioButtonField(             id: Weight.kg.rawValue,             label: Weight.kg.rawValue,             isMarked: selectedId == Weight.kg.rawValue ? true : false,             callback: weightRadioGroupCallback         )     }          var weightRadioPoundsMajority: some View {         RadioButtonField(             id: Weight.pounds.rawValue,             label: Weight.pounds.rawValue,             isMarked: selectedId == Weight.pounds.rawValue ? true : false,             callback: weightRadioGroupCallback         )     }          func weightRadioGroupCallback(id: String) {         selectedId = id         callback(id)     } } //MARK:- Group of height Radio Buttons enum Height: String {     case cms = "cms"     case inches = "inches" } struct heightRadioButtonGroups: View {     let callback: (String) -> ()              @State var selectedId: String = ""               var body: some View {         HStack {             heightRadioCmsMajority             heightRadioInchesMajority         }.padding(.leading, 8)     }          var heightRadioCmsMajority: some View {         RadioButtonField(             id: Height.cms.rawValue,             label: Height.cms.rawValue,             isMarked: selectedId == Height.cms.rawValue ? true : false,             callback: heightRadioGroupCallback         )     }          var heightRadioInchesMajority: some View {         RadioButtonField(             id: Height.inches.rawValue,             label: Height.inches.rawValue,             isMarked: selectedId == Height.inches.rawValue ? true : false,             callback: heightRadioGroupCallback         )     }          func heightRadioGroupCallback(id: String) {         selectedId = id         callback(id)     } }      //MARK:- Group of creatinine Radio Buttons enum Creatinine: String {     case mg = "mg/dL"     case umol = "\u{03BC}mol/L" } struct creatinineRadioButtonGroups: View {     let callback: (String) -> ()          @State var selectedId: String = ""          var body: some View {         HStack {             creatinineRadioMgMajority             creatinineRadioUmolMajority         }     }          var creatinineRadioMgMajority: some View {         RadioButtonField(             id: Creatinine.mg.rawValue,             label: Creatinine.mg.rawValue,             isMarked: selectedId == Creatinine.mg.rawValue ? true : false,             callback: creatinineRadioGroupCallback         )     }          var creatinineRadioUmolMajority: some View {         RadioButtonField(             id: Creatinine.umol.rawValue,             label: Creatinine.umol.rawValue,             isMarked: selectedId == Creatinine.umol.rawValue ? true : false,             callback: creatinineRadioGroupCallback         )     }          func creatinineRadioGroupCallback(id: String) {         selectedId = id         callback(id)     } } struct RadioButtonFieldView: View {     var body: some View {         HStack {             Text("Sex:")                 .font(Font.headline)             RadioButtonGroups { selected in                 return                 //print("Selected Sex is: (selected)")                              }         }.padding()                  HStack {             Text("Race:")                 .font(Font.headline)             raceRadioButtonGroups { selected in                 return                 //print("Selected Race is: (selected)")             }         }.padding()                           HStack {             Text("Weight:")                 .font(Font.headline)                weightRadioButtonGroups { selected in                 return                 //print("Selected Weight is: (selected)")             }         }.padding()                  HStack {             Text("Height:")                 .font(Font.headline)                   heightRadioButtonGroups { selected in                 return                // print("Selected Height is: (selected)")             }         }.padding()                  HStack {             Text("Creatinine:")                 .font(Font.headline)             creatinineRadioButtonGroups { selected in                 return                // print("Selected Creatinine is: (selected)")             }         }.padding()     }               } struct RadioButtonField_Previews: PreviewProvider {
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21