Hi everyone, List not updated after adding a new item from other view.
struct HistoryIView: View {
@StateObject var historyData1 = historyData()
private var totalAmount: Double {
historyData1.item.reduce(0.0) { partialResult, item in
partialResult + item.price
// also can be expressed as { $0 + $1.amount }
}
}
var body: some View {
NavigationView {
List {
Section(header: Text("Invoice History \(totalAmount.formatted(.currency(code: Locale.current.currency?.identifier ?? "BD")))")) {
ForEach(historyData1.item, id: \.id) { it in
HStack {
VStack(alignment: .leading) {
Text(it.cus)
Text(it.cusNum)
Text(it.dat.formatted(date: .abbreviated, time: .shortened))
.foregroundColor(Color("grayed"))
}
Spacer()
VStack {
HStack {
Text("Recipt#:")
.foregroundColor(.red)
Text("\(it.invNum)")
}
Text(it.price, format: .currency(code: Locale.current.currency?.identifier ?? "BD"))
.foregroundColor(.green)
}
}
}
.onDelete(perform: removeItem)
}
}
.navigationTitle("Invoice history")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("Delete All") {
historyData1.item.removeAll()
}
.tint(.red)
}
}
}
}
func removeItem(at offsets: IndexSet) {
itemsData.items.remove(atOffsets: offsets)
}
}
the model :
struct HistoryData: Identifiable, Codable {
var id = UUID()
let storN: String
let storNum: String
let invNum: String
let dat: Date
let cus: String
let cusAddr: String
let cusNum: String
let price: Double
}
class historyData: ObservableObject {
@Published var item: [HistoryData] = [] {
// FOR STORE DATA IN THE UserDefaults.
didSet {
let encoder = JSONEncoder()
if let encode = try? encoder.encode(item) {
UserDefaults.standard.set(encode, forKey: "HistoryData")
}
}
}
// FOR LOAD DATA FROM UserDefaults.
init() {
if let savedItems = UserDefaults.standard.data(forKey: "HistoryData") {
if let decodedItems = try? JSONDecoder().decode([HistoryData].self, from: savedItems) {
item = decodedItems
return
}
}
item = []
}
}
How to fix this issue?
Thanks in advance
Post
Replies
Boosts
Views
Activity
Hi everyone, I following 100 days of SwiftUI course , I used same idea of iExpense project exactly but without .sheet method, The list isn't updating when I add new item from AddView.
struct MainView: View {
var body: some View {
TabView {
ContentView()
.tabItem {
Image(systemName: "1.lane")
Text("One")
}
AddView(expenses: Expenses())
.tabItem {
Image(systemName: "2.lane")
Text("Two")
}
}
}
}
other codes same as iExpense project...
How to fix that ?
The project in GitHub: https://github.com/mahoozi97/iExpense-project
Thanks in advance
Hi everyone, I'm working on a measurements app , and I would like to set measuring near the object , Looking at the image, I mentioned the location of each measurement in the object.
I tried to use position(x: , y:) but that doesn't work well on all screen sizes. Can anyone suggest a solution?
my code:
VStack(spacing: 10) {
Text("J: \(Joint.formatted())")
HStack(spacing: 15) {
Text("L1: \(L1.formatted())")
Text("W1: \(W1.formatted())")
Text("L2: \(L2.formatted())")
Text("W2: \(W2.formatted())")
}
HStack(spacing: 20) {
Text("F: \(F.formatted())")
Text("H: \(H.formatted())")
Text("F: \(F.formatted())")
}
GeometryReader { geometry in
Image("test6")
.resizable()
.scaledToFit()
.frame(maxWidth: geometry.size.width * 1)
.padding()
}
}
thanks in advance.
Hi everyone, I'm working on a measurements app, and I would like to set measuring in the object, looking at the image.
That screenshot from iPhone 14 pro max, but when I run the app in iPhone 14 pro, the view is very bad. How can I solve that issue?
My code:
ZStack {
GeometryReader { geo in
Image("test3")
.resizable()
.scaledToFit()
.frame(maxWidth: geo.size.width * 1)
.padding()
HStack(spacing: 40) {
Text(Joint.formatted())
.position(x: geo.size.width - 383, y: geo.size.height - 350)
Text(W1.formatted())
.position(x: geo.size.width - 405, y: geo.size.height - 350)
Text(L1.formatted())
.position(x: geo.size.width - 382, y: geo.size.height - 350)
Text(W2.formatted())
.position(x: geo.size.width - 365, y: geo.size.height - 350)
Text(L2.formatted())
.position(x: geo.size.width - 350, y: geo.size.height - 350)
}
}
}
Thanks in advance
Hi everyone, I use this method for render a SwiftUI view to a PDF, My method is work and my app not crashing but I got errors on the debug area. The errors appearing exactly when I tap ShareLink button.
my code:
struct ContentView: View {
var body: some View { ...
// Rest of the codes
}
}
.navigationTitle("")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItemGroup {
ShareLink("Export PDF", item: render())
}
}
}
func render() -> URL {
// 1: Render Hello World with some modifiers
let renderer = ImageRenderer(content: PdfView)
// 2: Save it to our documents directory
let url = URL.documentsDirectory.appending(path: "test.pdf")
// 3: Start the rendering process
renderer.render { size, context in
// 4: Tell SwiftUI our PDF should be the same size as the views we're rendering
var box = CGRect(x: 0, y: 0, width: size.width, height: size.height)
// 5: Create the CGContext for our PDF pages
guard let pdf = CGContext(url as CFURL, mediaBox: &box, nil) else {
return
}
// 6: Start a new PDF page
pdf.beginPDFPage(nil)
// 7: Render the SwiftUI view data onto the page
context(pdf)
// 8: End the page and close the file
pdf.endPDFPage()
pdf.closePDF()
}
return url
}
var PdfView: some View { ...
// Rest of the code
}
}
}
}
Errors :
How I can solve that?
Thanks in advance
Hi everyone, I facing two issues with Xcode 15:
1- slow performance especially when trying to test the app on simulator or physical device, I tried to used simulator running iOS 16.4, also the preview take a long time to load.
2- I am Beginner and Xcode 15 not catching my mistakes immediately same as Xcode 14
My device is Mac Air M2 8GB (Sonoma 14.1.1)
Hi everyone,
I received this email from apple before 10 days:
The information or attachments you provided were not sufficient enough for us to conduct a complete compliance check. To avoid your App Store Connect account from being disabled, provide the information again in a clear and readable format.
I try to provide information, but I facing this issue:
How can I solve that?