Code for this view
struct CreateDividendView: View {
@Environment(\.presentationMode) var presentationMode
@Environment(\.colorScheme) var colorScheme
@ObservedObject var exchange: StockExchange
@ObservedObject var stock: Stock
@StateObject var dividend = Dividend()
@State private var showingAlert = false
@State private var showingDateAlert = false
var isNew = true
var numberFormatter: NumberFormatter = {
let nf = NumberFormatter()
nf.locale = Locale.current
nf.numberStyle = .decimal
return nf
}()
var decimalFormatter: NumberFormatter = {
let df = NumberFormatter()
df.locale = Locale.current
df.numberStyle = .decimal
df.generatesDecimalNumbers = true
return df
}()
var body: some View {
NavigationView {
VStack(spacing: 0) {
HStack(spacing: 0) {
Text(stock.ticker)
.font(.title)
.bold()
Spacer()
}
.padding(.horizontal).padding(.horizontal, 5)
HStack(spacing: 0) {
Text(stock.name.uppercased())
.font(.footnote)
.foregroundColor(.gray)
Spacer()
}
.padding(.horizontal).padding(.horizontal, 5)
HStack(spacing: 0) {
Text("Dividend Value")
.padding(.leading, 10)
Spacer()
Text("$\(Double(dividend.quantity ?? 0) * (dividend.value ?? 0) - (dividend.fees ?? 0), specifier: "%.2f")")
.padding(.trailing, 10)
}
.padding(.horizontal, 10).padding(.vertical, 10)
.background(Color(UIColor.systemGray2))
.cornerRadius(8)
.padding(.top, 20).padding(.horizontal, 20)
Form {
HStack {
DatePicker("Date", selection: $dividend.date, displayedComponents: [.date])
.datePickerStyle(.compact)
}
HStack {
Text("Quantity")
TextField("100", value: $dividend.quantity, formatter: numberFormatter)
.multilineTextAlignment(.trailing)
.keyboardType(.numberPad)
}
HStack {
Text("Value")
TextField("1.234", value: $dividend.value, formatter: decimalFormatter)
.multilineTextAlignment(.trailing)
.keyboardType(.decimalPad)
}
HStack {
Text("Fees")
TextField("1.00", value: $dividend.fees, formatter: decimalFormatter)
.multilineTextAlignment(.trailing)
.keyboardType(.decimalPad)
}
}
if !isNew {
Button(action: {
showingAlert = true
}) {
Text("\(Image(systemName: "trash.fill")) Delete Dividend")
.font(.title3)
.foregroundColor(.white)
.frame(width: UIScreen.main.bounds.width/1.2, height: 35, alignment: .center)
.padding(.horizontal, 10).padding(.vertical, 5)
.background(Color.red)
.cornerRadius(10)
}
.padding(8)
}
Spacer()
}
.navigationTitle(isNew ? "Record Dividend" : "Edit Dividend")
.navigationBarTitleDisplayMode(.inline)
.background(colorScheme == .dark ? Color(UIColor.systemBackground) : Color(UIColor.secondarySystemBackground))
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(action: {
presentationMode.wrappedValue.dismiss()
}) {
Text("Cancel")
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
if dividend.date > Date.now {
showingDateAlert = true
} else {
stock.saveDividend(dividend: dividend)
exchange.updateValues()
DataController.shared.saveData()
presentationMode.wrappedValue.dismiss()
}
}) {
Text("Save")
.bold()
}
}
}
.alert(isPresented: $showingDateAlert) {
Alert(
title: Text("Date is in the future"),
primaryButton: .default(Text("Save anyway")) {
stock.saveDividend(dividend: dividend)
DataController.shared.saveData()
presentationMode.wrappedValue.dismiss()
},
secondaryButton: .cancel()
)
}
.alert(isPresented: $showingAlert) {
Alert(
title: Text("Are you sure?"),
primaryButton: .destructive(Text("Delete")) {
stock.deleteDividend(dividend: dividend)
},
secondaryButton: .cancel()
)
}
}
}
}
Post
Replies
Boosts
Views
Activity
@Claude31 I have tested using the simulator in iOS 15.2 and in iOS 15.4 and found the following:
iOS 15.2 works as expected, values formatted when moving to the next field, accepted, calculated and stored correctly.
iOS 15.4 has the issues described above. values not formatting when moving to the next field, not accepted (remain nil), not calculated and not stored.
I wonder if something has changed in the formatter in iOS 15.4?
Any help is greatly appreciated
Hi @Claude31
The issue is not with the Text() & specifier it is with the TextField()
TextField("1.234", value: $dividend.value, formatter: decimalFormatter)
where the decimal formatter seems to not work.
Changing to
TextField("1.234", value: $dividend.value, format: .number)
seems to have fixed the problem.
I made a sample project which I submitted to Apple Feedback (FB9963196)
import SwiftUI
class Data: ObservableObject {
@Published var id = UUID().uuidString
@Published var value1: Double?
@Published var value2: Int?
}
struct ContentView: View {
@ObservedObject var data = Data()
var numberFormatter: NumberFormatter = {
let nf = NumberFormatter()
nf.locale = Locale.current
nf.numberStyle = .decimal
return nf
}()
var decimalFormatter: NumberFormatter = {
let df = NumberFormatter()
df.locale = Locale.current
df.numberStyle = .decimal
df.generatesDecimalNumbers = true
return df
}()
var body: some View {
let calc = data.value1 ?? 0 * Double(data.value2 ?? 0)
Form {
HStack {
Text("Value 1")
TextField("1.234", value: $data.value1, formatter: decimalFormatter)
.multilineTextAlignment(.trailing)
.keyboardType(.decimalPad)
}
HStack {
Text("Value 2")
TextField("10", value: $data.value2, formatter: numberFormatter)
.multilineTextAlignment(.trailing)
.keyboardType(.decimalPad)
}
Section {
Text("Calculated value = \(calc)")
}
}
}
}
if you run this in the simulator in iOS15.2 and 15.4 you will see the issue with the TextField.
As Sam NZ above has indicated that this is due to the value being optional in his project.