SwiftUI - ObservableObject, EnvironmentObject, SummaryView - view

After creating the code, i would like to create a summary view that allows me to view the values that have been chosen in the picker.
How can I do? I read some forums about #ObservableObject and about #EnvironmentObject, but I can't understand ...
Thanks very much :)
import SwiftUI
//SUMMARYPAGE
struct SummaryView: View {
var body: some View {
NavigationView {
Form {
VStack(alignment: .leading, spacing: 6) {
Text("First Animal: \("firstAnimalSelected")")
Text("First Animal: \("secondAnimalSelected")")
}
}
}
}
}
struct SummaryView_Previews: PreviewProvider {
static var previews: some View {
SummaryView()
}
}
enum Animal: String, CaseIterable {
case select
case bear
case cat
case dog
case lion
case tiger
}
struct ContentView: View {
#State private var firstAnimal = Animal.allCases[0]
#State private var secondAnimal = Animal.allCases[0]
var body: some View {
NavigationView {
Form {
Section(header: Text("Animals")
.foregroundColor(.black)
.font(.system(size: 15))
.fontWeight(.bold)) {
Picker(selection: $firstAnimal, label: Text("Select first animal")) {
ForEach(Animal.allCases, id: \.self) { element in
Text(element.rawValue.capitalized)
}
}
Picker(selection: $secondAnimal, label: Text("Select second animal")) {
ForEach(Animal.allCases.filter { $0 != firstAnimal || firstAnimal == .select }, id: \.self) { element2 in
Text(element2.rawValue.capitalized)
}
}
}.font(.system(size: 15))
}.navigationBarTitle("List", displayMode: .inline)
}
}
}

You can move your #State properties to an ObservableObject:
class ViewModel: ObservableObject {
#Published var firstAnimal = Animal.allCases[0]
#Published var secondAnimal = Animal.allCases[0]
}
and access them from an #EnvironmentObject:
struct ContentView: View {
#EnvironmentObject var viewModel: ViewModel
var body: some View {
...
Picker(selection: $viewModel.firstAnimal, label: Text("Select first animal")) {
ForEach(Animal.allCases, id: \.self) { element in
Text(element.rawValue.capitalized)
}
}
}
}
struct SummaryView: View {
#EnvironmentObject var viewModel: ViewModel
var body: some View {
NavigationView {
Form {
VStack(alignment: .leading, spacing: 6) {
Text("First Animal: \(viewModel.firstAnimal.rawValue)")
Text("Second Animal: \(viewModel.secondAnimal.rawValue)")
}
}
}
}
}
Remember to inject your ViewModel to your root view:
ContentView().environmentObject(ViewModel())

Related

"Result of 'MainView' initializer is unused" -- trying to filter NavigationView in SwiftUI

I am trying to piece together a custom NavigationView in SwiftUI with Buttons for filtering. In this simple step I am just trying to change '.navigationTitle(title)' through filtering with three buttons ('header1', 'header2', 'header3'), however it is not working.
You can find a visual of this code here
ContentView:
struct ContentView: View {
var body: some View {
MainView(filter: .header1)
}
}
MainView:
struct MainView: View {
enum SourceType {
case header1, header2, header3
}
#EnvironmentObject var sourceItems: SourceItems
let filter: SourceType
var body: some View {
NavigationView {
List {
HeaderView()
ForEach(0..<10) { sourceItem in
NavigationLink(destination: Text("Destination")) {
Text("Item: \(sourceItem)")
}
}
}
.listStyle(.inset)
.navigationTitle(title) // I am trying to make this change
}
}
var title: String {
switch filter {
case .header1:
return "Header 1"
case .header2:
return "Header 2"
case .header3:
return "Header 3"
}
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView(filter: .header1)
.environmentObject(SourceItems())
}
}
HeaderView (This is where I get the warnings):
struct HeaderView: View {
#StateObject var sourceItems = SourceItems()
var body: some View {
HStack {
Button(action: {
MainView(filter: .header1)
}, label: {
Text("Header 1")
})
.buttonStyle(PlainButtonStyle())
Spacer()
Button(action: {
MainView(filter: .header2)
}, label: {
Text("Header 2")
})
.buttonStyle(PlainButtonStyle())
Spacer()
Button(action: {
MainView(filter: .header3)
}, label: {
Text("Header 3")
})
.buttonStyle(PlainButtonStyle())
}
.environmentObject(sourceItems)
}
}
The code runs but the buttons don't filter.
Thanks for any help (I am new to SwiftUI)!

How do I create another unique view for each device?

How do I create another view when the user chooses a device ? For each device, the view will not be the same because the information given will not be the same ( custom view )
Thank you to put me on the track.
Canvas image
struct ContentView: View {
var body: some View {
NavigationView {
List(Ios, children: \.sousMenuIos) { item in
HStack {
Image(item.image)
.resizable()
.scaledToFit()
.frame(width: 60, height: 60)
Text(item.name)
.font(.system(.title3, design: .rounded))
.bold()
}
}
.navigationTitle("Jailbreak")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
You need to wrap your list label in a NavigationLink and pass the item to the costum view that you want. here is a working example with dummy data.
struct ListItems: View {
var body: some View {
NavigationView {
List(iOS.previewData) { item in
NavigationLink(destination: CostumView(text: item.name)){
HStack {
Image(systemName: item.image)
.resizable()
.scaledToFit()
.frame(width: 60, height: 60)
Text(item.name)
.font(.system(.title3, design: .rounded))
.bold()
}
}
}
.navigationTitle("Jailbreak")
}
}
}
struct CostumView: View {
var text: String
var body: some View{
Text(text)
}
}
struct ListItems_Previews: PreviewProvider {
static var previews: some View {
ListItems()
}
}
struct iOS: Identifiable {
var id = UUID()
var image: String = ""
var name: String
static let previewData = [
iOS(image: "phone", name: "iPhone 8"),
iOS(image: "phone", name: "iPhone X")
]
}

Updating the contents of an array from a different view

I'm writing a macOS app in Swiftui, for Big Sur and newer. It's a three pane navigationview app, where the left most pane has the list of options (All Notes in this case), the middle pane is a list of the actual items (title and date), and the last one is a TextEditor where the user adds text.
Each pane is a view that calls the the next view via a NavigationLink. Here's the basic code for that.
struct NoteItem: Codable, Hashable, Identifiable {
let id: Int
var text: String
var date = Date()
var dateText: String {
dateFormatter.dateFormat = "EEEE, MMM d yyyy, h:mm a"
return dateFormatter.string(from: date)
}
var tags: [String] = []
}
struct ContentView: View {
#State var selection: Set<Int> = [0]
var body: some View {
NavigationView {
List(selection: self.$selection) {
NavigationLink(destination: AllNotes()) {
Label("All Notes", systemImage: "doc.plaintext")
}
.tag(0)
}
.listStyle(SidebarListStyle())
.frame(minWidth: 100, idealWidth: 150, maxWidth: 200, maxHeight: .infinity)
Text("Select a note...")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
}
struct AllNotes: View {
#State var items: [NoteItem] = {
guard let data = UserDefaults.standard.data(forKey: "notes") else { return [] }
if let json = try? JSONDecoder().decode([NoteItem].self, from: data) {
return json
}
return []
}()
#State var noteText: String = ""
var body: some View {
NavigationView {
List(items) { item in
NavigationLink(destination: NoteView()) {
VStack(alignment: .leading) {
Text(item.text.components(separatedBy: NSCharacterSet.newlines).first!)
Text(item.dateText).font(.body).fontWeight(.light)
}
.padding(.vertical, 8)
}
}
.listStyle(InsetListStyle())
Text("Select a note...")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.navigationTitle("A title")
.toolbar {
ToolbarItem(placement: .navigation) {
Button(action: {
NewNote()
}) {
Image(systemName: "square.and.pencil")
}
}
}
}
struct NoteView: View {
#State var text: String = ""
var body: some View {
HStack {
VStack(alignment: .leading) {
TextEditor(text: $text).padding().font(.body)
.onChange(of: text, perform: { value in
print("Value of text modified to = \(text)")
})
Spacer()
}
Spacer()
}
.padding()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.white)
}
}
When I create a new note, how can I save the text the user added on the TextEditor in NoteView in the array loaded in AllNotes so I could save the new text? Ideally there is a SaveNote() function that would happen on TextEditor .onChange. But again, given that the array lives in AllNotes, how can I update it from other views?
Thanks for the help. Newbie here!
use EnvironmentObject in App
import SwiftUI
#main
struct NotesApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(DataModel())
}
}
}
now DataModel is a class conforming to ObservableObject
import SwiftUI
final class DataModel: ObservableObject {
#AppStorage("notes") public var notes: [NoteItem] = []
}
any data related stuff should be done in DataModel not in View, plus you can access it and update it from anywhere, declare it like this in your ContentView or any child View
NoteView
import SwiftUI
struct NoteView: View {
#EnvironmentObject private var data: DataModel
var note: NoteItem
#State var text: String = ""
var body: some View {
HStack {
VStack(alignment: .leading) {
TextEditor(text: $text).padding().font(.body)
.onChange(of: text, perform: { value in
guard let index = data.notes.firstIndex(of: note) else { return }
data.notes[index].text = value
})
Spacer()
}
Spacer()
}
.padding()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.white)
.onAppear() {
print(data.notes.count)
}
}
}
AppStorage is the better way to use UserDefaults but AppStorage does not work with custom Objects yet (I think it does for iOS 15), so you need to add this extension to make it work.
import SwiftUI
struct NoteItem: Codable, Hashable, Identifiable {
let id: UUID
var text: String
var date = Date()
var dateText: String {
let df = DateFormatter()
df.dateFormat = "EEEE, MMM d yyyy, h:mm a"
return df.string(from: date)
}
var tags: [String] = []
}
extension Array: RawRepresentable where Element: Codable {
public init?(rawValue: String) {
guard let data = rawValue.data(using: .utf8),
let result = try? JSONDecoder().decode([Element].self, from: data)
else {
return nil
}
self = result
}
public var rawValue: String {
guard let data = try? JSONEncoder().encode(self),
let result = String(data: data, encoding: .utf8)
else {
return "[]"
}
return result
}
}
Now I changed AllNotes view to work with new changes
struct AllNotes: View {
#EnvironmentObject private var data: DataModel
#State var noteText: String = ""
var body: some View {
NavigationView {
List(data.notes) { note in
NavigationLink(destination: NoteView(note: note)) {
VStack(alignment: .leading) {
Text(note.text.components(separatedBy: NSCharacterSet.newlines).first!)
Text(note.dateText).font(.body).fontWeight(.light)
}
.padding(.vertical, 8)
}
}
.listStyle(InsetListStyle())
Text("Select a note...")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.navigationTitle("A title")
.toolbar {
ToolbarItem(placement: .navigation) {
Button(action: {
data.notes.append(NoteItem(id: UUID(), text: "New Note", date: Date(), tags: []))
}) {
Image(systemName: "square.and.pencil")
}
}
}
}
}

The picker doesn't update the number of segments it has when a value is changed during execution

I have recently started learning swiftUI and I'm facing some issues here. This is the code:
struct ContentView: View {
#State private var measurementType = 0
#State private var inputValue = ""
#State private var inputUnit = 0
#State private var outputUnit = 1
var outputValue = ""
let measurementTypes = ["Temp", "Length", "Time", "Volume"]
var typeDictionary = [
["Celsius", "Fahrenheit", "Kelvin"],
["Meters", "Kilometers", "Feet", "Yards", "Miles"],
["Seconds", "Minutes", "Hours", "Days"],
["Milliliters", "Liters", "Cups", "Pints", "Gallons"]
]
var body: some View {
NavigationView {
Form {
Section(header: Text("Choose the type of measurement")) {
Picker("The type of measurement", selection: $measurementType) {
ForEach(0 ..< measurementTypes.count) {
Text("\(measurementTypes[$0])")
}
}
.id(measurementType)
.pickerStyle(SegmentedPickerStyle())
}
Section {
TextField("Enter the value", text: $inputValue)
.keyboardType(.decimalPad)
}
Section(header: Text("Choose the input unit")) {
Picker("The input unit", selection: self.$inputUnit) {
ForEach(0 ..< typeDictionary[measurementType].count) {
Text("\(typeDictionary[measurementType][$0])")
}
}
.pickerStyle(SegmentedPickerStyle())
}
Section(header: Text("Choose the output unit")) {
}
Section(header: Text("Converted Value")) {
Text("")
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Whenever I change the measurementType while the code is running, the values of the picker in the input units section update, but the number of segments don't. If I start with the value of measurementType as 1, then the app simply crashes when I choose Temp or Time from the picke.
When using ForEach, you need to provide an id so SwiftUI can uniquely identify each element in the array and know when to redraw the view. You can use id: \.self to use the item's value as its id.
Here's an updated working version:
import SwiftUI
struct ContentView: View {
#State private var measurementType = 0
#State private var inputValue = ""
#State private var inputUnit = 0
#State private var outputUnit = 1
var outputValue = ""
let measurementTypes = ["Temp", "Length", "Time", "Volume"]
var typeDictionary = [
["Celsius", "Fahrenheit", "Kelvin"],
["Meters", "Kilometers", "Feet", "Yards", "Miles"],
["Seconds", "Minutes", "Hours", "Days"],
["Milliliters", "Liters", "Cups", "Pints", "Gallons"]
]
var body: some View {
NavigationView {
Form {
Section(header: Text("Choose the type of measurement")) {
Picker("The type of measurement", selection: $measurementType) {
ForEach(0 ..< measurementTypes.count, id: \.self) {
Text("\(measurementTypes[$0])")
}
}
.id(measurementType)
.pickerStyle(SegmentedPickerStyle())
}
Section {
TextField("Enter the value", text: $inputValue)
.keyboardType(.decimalPad)
}
Section(header: Text("Choose the input unit")) {
Picker("The input unit", selection: self.$inputUnit) {
ForEach(0 ..< typeDictionary[measurementType].count, id: \.self) {
Text("\(typeDictionary[measurementType][$0])")
}
}
.pickerStyle(SegmentedPickerStyle())
}
Section(header: Text("Choose the output unit")) {
}
Section(header: Text("Converted Value")) {
Text("")
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

SwiftUI on macOS: list with detail view and multiple selection

TL;DR:
I cannot have a list with a detail view and multiple selections on macOS.
In more detail:
For demonstration purposes of my issue, I made a small example project. The UI looks as follows:
This is the "app" when launched, with a list on top and a detail representation below. Because I am using the List's initialiser init(_:selection:rowContent:), where selection is of type Binding<SelectionValue?>? according to Apple's documentation, I get selecting items with the keyboard arrow keys for free.
Here's the complete code:
import SwiftUI
#main
struct UseCurorsInLisstApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(ViewModel())
}
}
}
class ViewModel: ObservableObject {
#Published var items = [Item(), Item(), Item(), Item(), Item()]
#Published var selectedItem: Item? = nil
}
struct Item: Identifiable, Hashable {
let id = UUID()
}
struct ContentView: View {
#EnvironmentObject var vm: ViewModel
var body: some View {
VStack {
List(vm.items, id: \.self, selection: $vm.selectedItem) { item in
VStack {
Text("Item \(item.id.uuidString)")
Divider()
}
}
Divider()
Group {
if let item = vm.selectedItem {
Text("Detail item \(item.id.uuidString)")
} else {
Text("No selection…")
}
}
.frame(minHeight: 200.0, maxHeight: .infinity)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Now, having had success with this so far, I figured being able to select more than one row would be useful, so I took a closer look into List(_:selection:rowContent:), where selection is of type Binding<Set<SelectionValue>>?. To be able to have a detail view, I just made a few minor changes to
the ViewModel:
class ViewModel: ObservableObject {
#Published var items = [Item(), Item(), Item(), Item(), Item()]
#Published var selectedItem: Item? = nil
#Published var selectedItems: Set<Item>? = nil {
didSet {
if selectedItems?.count == 1, let item = selectedItems?.first {
selectedItem = item
}
}
}
}
and the ContentView:
struct ContentView: View {
#EnvironmentObject var vm: ViewModel
var body: some View {
VStack {
List(vm.items, id: \.self, selection: $vm.selectedItems) { item in
VStack {
Text("Item \(item.id.uuidString)")
Divider()
}
}
Divider()
Group {
if vm.selectedItems?.count == 1, let item = vm.selectedItems?.first {
Text("Detail item \(item.id.uuidString)")
} else {
Text("No or multiple selection…")
}
}
.frame(minHeight: 200.0, maxHeight: .infinity)
}
}
}
The problem now is that I cannot select an item of the row any more, neither by clicking, nor by arrow keys. Is this a limitation I am running into or am I "holding it wrong"?
Use the button and insert it into the set. Keyboard selection also works with shift + (up/down arrow)
class ViewModel: ObservableObject {
#Published var items = [Item(), Item(), Item(), Item(), Item()]
#Published var selectedItem: Item? = nil
#Published var selectedItems: Set<Item> = []
}
struct ContentView: View {
#EnvironmentObject var vm: ViewModel
var body: some View {
VStack {
List(vm.items, id: \.self, selection: $vm.selectedItems) { item in
Button {
vm.selectedItem = item
vm.selectedItems.insert(item)
} label: {
VStack {
Text("Item \(item.id.uuidString)")
Divider()
}
}
.buttonStyle(PlainButtonStyle())
}
Divider()
Group {
if let item = vm.selectedItem {
Text("Detail item \(item.id.uuidString)")
} else {
Text("No or multiple selection…")
}
}
.frame(minHeight: 200.0, maxHeight: .infinity)
}
}
}
Add remove:
Button {
vm.selectedItem = item
if vm.selectedItems.contains(item) {
vm.selectedItems.remove(item)
} else {
vm.selectedItems.insert(item)
}
}
Edit
In simple need to give a blank default value to set. because in nil it will never append to set need initialization.
#Published var selectedItems: Set<Item> = [] {
Actually my error was pretty dumb – making the selectedItems-set optional prevents the list from working correctly. Shoutout to #Raja Kishan, who pushed me into the right direction with his proposal.
Here's the complete working code:
import SwiftUI
#main
struct UseCurorsInLisstApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(ViewModel())
}
}
}
class ViewModel: ObservableObject {
#Published var items = [Item(), Item(), Item(), Item(), Item()]
#Published var selectedItems = Set<Item>()
}
struct Item: Identifiable, Hashable {
let id = UUID()
}
struct ContentView: View {
#EnvironmentObject var vm: ViewModel
var body: some View {
VStack {
List(vm.items, id: \.self, selection: $vm.selectedItems) { item in
VStack {
Text("Item \(item.id.uuidString)")
Divider()
}
}
Divider()
Group {
if vm.selectedItems.count == 1, let item = vm.selectedItems.first {
Text("Detail item \(item.id.uuidString)")
} else {
Text("No or multiple selection…")
}
}
.frame(minHeight: 200.0, maxHeight: .infinity)
}
}
}

Resources