Hello, I'm new to app development and have a question that I have been searching all over the place to find an answer to. I have followed Apple's own guides and don't know how to move around the strange issue I am experiencing.
I have a project using NavigationSplitView and have the code setup so that it navigates through an array of items. The two issues I am experiencing that I hope to find some help with are:
I would like to be able to have my add button, instead of presenting a sheet, just navigate to a new item within the NavigationSplitView. This would be more natural for the type of data application I have created. At this time I create the item with a .sheet modifier and then the item is created and added to the array. However, I then have to tap on the newly created item to get it to be selected. Is this how SwiftUI should be working?
Whenever an item is deleted from the list, and it was the last item selected or the last item in the array, the NavigationSplitView will continue showing that remaining item. The only way to get it to show an empty content screen is to force close the application and then reopen it.
Since this is my first time posting, I'm not sure what information would be helpful, but would be happy to provide anything which could be of assistance. :-)
Post
Replies
Boosts
Views
Activity
Hello I'm a new developer and am learning the ropes. I have an app that I'm testing and seem to have run into a bug. The data is syncing from one device to another, however it takes closing the app on the Mac or force closing the app on iOS/iPadOS to get the app to reflect the new data.
Is there specific code I code share to help solve this issue or any suggestions that someone may have? Thank you ahead of time for your assistance.
import SwiftData
@main
struct ApplicantProcessorApp: App {
var sharedModelContainer: ModelContainer = {
let schema = Schema([
Applicant.self,
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return try ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(sharedModelContainer)
}
}
struct ContentView: View {
var body: some View {
FilteredApplicantListView()
}
}
#Preview {
ContentView()
.modelContainer(SampleData.shared.modelContainer)
}
struct FilteredApplicantListView: View {
@State private var searchText = ""
var body: some View {
NavigationSplitView {
ApplicantListView(applicantFilter: searchText)
.searchable(text: $searchText, prompt: "Enter Name, Email, or Phone Number")
.autocorrectionDisabled(true)
} detail: { }
}
}
import SwiftData
struct ApplicantListView: View {
@Environment(\.modelContext) private var modelContext
@Query private var applicants: [Applicant]
@State private var newApplicant: Applicant?
init(applicantFilter: String = "") {
// Filters
}
var body: some View {
Group {
if !applicants.isEmpty {
List {
ForEach(applicants) { applicant in
NavigationLink {
ApplicantView(applicant: applicant)
} label: {
HStack {
VStack {
HStack {
Text(applicant.name)
Spacer()
}
HStack {
Text(applicant.phoneNumber)
.font(.caption)
Spacer()
}
HStack {
Text(applicant.email)
.font(.caption)
Spacer()
}
HStack {
Text("Expires: \(formattedDate(applicant.expirationDate))")
.font(.caption)
Spacer()
}
}
if applicant.applicationStatus == ApplicationStatus.approved {
Image(systemName: "checkmark.circle")
.foregroundStyle(.green)
.font(.title)
} else if applicant.applicationStatus == ApplicationStatus.declined {
Image(systemName: "xmark.circle")
.foregroundStyle(.red)
.font(.title)
} else if applicant.applicationStatus == ApplicationStatus.inProgress {
Image(systemName: "hourglass.circle")
.foregroundStyle(.yellow)
.font(.title)
} else if applicant.applicationStatus == ApplicationStatus.waitingForApplicant {
Image(systemName: "person.circle")
.foregroundStyle(.yellow)
.font(.title)
} else {
Image(systemName: "yieldsign")
.foregroundStyle(.yellow)
.font(.title)
}
}
}
}
.onDelete(perform: deleteItems)
}
} else {
ContentUnavailableView {
Label("No Applicants", systemImage: "pencil.fill")
}
}
}
.navigationTitle("Applicants")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
ToolbarItem {
Button(action: addApplicant) {
Label("Add Item", systemImage: "plus")
}
}
}
.sheet(item: $newApplicant) { applicant in
NavigationStack {
ApplicantView(applicant: applicant, isNew: true)
}
}
}
private func addApplicant() {
withAnimation {
let newItem = Applicant()
modelContext.insert(newItem)
newApplicant = newItem
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
for index in offsets {
modelContext.delete(applicants[index])
}
}
}
func formattedDate(_ date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .none
return dateFormatter.string(from: date)
}
}
import SwiftData
@Model
final class Applicant {
var name = ""
var email = ""
var phoneNumber = ""
var applicationDate = Date.now
var expirationDate: Date {
return Calendar.current.date(byAdding: .day, value: 90, to: applicationDate)!
}
Hello, I'm a new programmer here, so this may be an error on my part, however I have tried my best to research the issue and believe I may have discover a bug. I have a set of tabItems inside of a TabView using a variable to track the selected tab called selectedIndex. I have added in a text view to watch the selected tab. This works correctly in the canvas and in a simulator, however whenever I build this on an iOS Device or for my Mac the selectedIndex does not change when selecting tabs like it does in the canvas and simulator. Instead it just stays at the default 0. Any assistance would be great. :-)
import SwiftUI
struct ContentView: View {
@State private var selectedIndex = 0
var body: some View {
Text("\(selectedIndex)")
TabView(selection: $selectedIndex) {
FilteredApplicantListView()
.tabItem { Label("Applicant Processor", systemImage: "person.3.sequence.fill") }
.tag(0)
QuickFinancials()
.tabItem { Label("Quick Financials", systemImage: "dollarsign.gauge.chart.leftthird.topthird.rightthird") }
.tag(1)
MoveInView()
.tabItem { Label("Move Ins", systemImage: "figure.walk.arrival") }
.tag(2)
MoveOutView()
.tabItem { Label("Move Outs", systemImage: "figure.walk.departure") }
.tag(3)
}
}
}