Swift Notes on Navigation and Some Protocols

Xinyi Xiang
1 min readOct 14, 2020

You cannot put two alerts with isPresented in the same View.

@StateObject conforms to @ObservableObject protocol automatically. You could either pass the state object into a property with ObservedObject attribute or alternatively add using the .environmentObject(:_) modifier.

When separating the leading and trailing items in the NavigationBarItems, use commas.

The place of the .environment matters because you need to include whatever you send it to, otherwise it wouldn’t know what to modify.

NavigationView{List{ForEach(store.documents) { document inNavigationLink(destination: EmojiArtDocumentView(document: document).navigationBarTitle(self.store.name(for: document))){Text(self.store.name(for:document))}}.onDelete { indexSet inindexSet.map { self.store.documents[$0] }.forEach { document inself.store.removeDocument(document)}}}.navigationBarTitle(self.store.name).navigationBarItems(leading: Button(action: {self.store.addDocument()}, label: {Image(systemName: "plus").imageScale(.large)}),trailing: EditButton()).environment(\.editMode, $editMode)}

--

--