Hey!
Is there any opportunity in SwiftUI to keep a global variable that can changed its value and not cause views refresh (because I'm not going to display the value of this var)?
Post
Replies
Boosts
Views
Activity
Hi!
I'm learning SwiftUI and now got stuck with a problem of using @EnvironmentObject. The problem is: I get an email inputed by user and I have to pass it through several view (each view I call with NavigationLink). But I get an error sometimes:
struct RepeatPinView: View {
@State var pin: String
@State var pinRepeat: String = ""
@State var text: String = "Repeat PIN"
@State var selection: String? = nil
@EnvironmentObject var globalObj: GlobalObj
@Environment(\.colorScheme) var colorScheme
var body: some View {
let buttons: [[numPadButton]] = [
[.one, .two, .three],
[.four, .five, .six],
[.seven, .eight, .nine],
[.dop, .zero, .del],
]
VStack {
NavigationLink(destination: ContentView(), tag: "GotPin", selection: $selection) { EmptyView() }
Spacer()
// Text("Pin repeat \(globalObj.email)")
/* here I'm trying to check the email but sometimes and sometimes not (whaat.. can't understand how it works) I get a Fatal error: No ObservableObject of type GlobalObj found
*/
Text(text)
.font(.system(size: 30))
Spacer()
Text(pinRepeat)
.font(.system(size: 30))
.onChange(of: pinRepeat) { pinRepeat in
print(pinRepeat)
// print(globalObj.email)
/* same problem, sometimes get a Fatal error*/
if pinRepeat.count == 4 {
if self.pinRepeat == pin {
print(globalObj.email)
let textToWrite = globalObj.email + "\n" + pinRepeat
print(textToWrite)
/* surprisingly, but here everything works fine WHY??? */
selection = "GotPin"
} else {
text = "Try again"
}
}
}
.frame(width: 100, height: 50, alignment: .center)
Spacer()
ForEach(buttons, id: \.self) { row in
HStack {
ForEach(row, id: \.self) { item in
Button(action: {
switch item.rawValue {
case "dop":
print("dop")
case "del":
print("del")
if pinRepeat != "" {
pinRepeat.removeLast()
}
default:
pinRepeat.append(item.rawValue)
}
}, label: {
if item.rawValue == "del" {
Image(systemName: "delete.left")
.font(.title)
.foregroundColor(colorScheme == .dark ? .white : .black)
.padding()
} else if item.rawValue == "dop" {
Text("")
.font(.title)
.foregroundColor(colorScheme == .dark ? .white : .black)
.padding()
} else {
Text(item.rawValue)
.fontWeight(.bold)
.font(.largeTitle)
.foregroundColor(colorScheme == .dark ? .white : .black)
.padding()
.background(
Circle()
.stroke(Color.yellow, lineWidth: 4)
.frame(width: buttonWidth(item: item) - 5, height: buttonHeight(item: item) - 5, alignment: .center)
)
}
})
.frame(width: buttonWidth(item: item), height: buttonHeight(item: item), alignment: .center)
.padding([.trailing, .leading], 7)
}
}
.padding(.bottom, 7)
}
Spacer()
Spacer()
Spacer()
}
}
}
In previous views (there are three views before this one) I never got a Fatal error doing the same things.
Help me, please
Hey, everyone! Now I'm trying to do a simple app where I have a list of titles. I have two actions: tap the button "plus" to add new title and tap the row to edit the title. Each time I call "sheet". I use extra var to handle multiple sheets. So, I use "activeSheet" which changes when I tap the row and when I tap add button:
	@State var activeSheet = 0
@State var isPresented = false
@State var filmID: UUID = UUID()
var body: some View {
NavigationView {
List {
ForEach (modelData) { watch in
Row(film: watch)
.onTapGesture {
filmID = watch.id!
activeSheet = 1
isPresented = true
}
}
.sheet(isPresented: $isPresented) {
if (activeSheet == 1) {
DetailNotWatchedView(filmID: $filmID, isPresentedDetailView: $isPresented)
}
else if (activeSheet == 2) {
AddingNotWatchedView(isPresented: $isPresented)
}
}
.navigationBarTitle("To watch")
.navigationBarItems(trailing:
Button(action: {
activeSheet = 2
isPresented = true },
label: { Image(systemName: "plus.circle").imageScale(.large) }))
.listStyle(PlainListStyle())
}
The problem is that when I tap add button before tapping the row I go to DetailNotWatchedView instead of AddingNotWatchedView. I suppose when I tap add button activeSheet must change to 2. But if I tap the row before I tap the add button everything works OK. I can't understand how to fix it.
Hey, everyone! I've just started learning SwiftUI. Moreover, I upgraded Xcode yesterday so now it is Xcode 12.
My problem:
I need to make a list and locate it in NavigationView. Everything is OK until I'm trying to add NavigationBarItems. When I add NavigationBarItems the extra grey-colored space around list appears. Probably it is because of Xcode 12. Help me, please.
My code:
import SwiftUI
struct ContentView: View {
var body: some View {
let f1 = Film(id: UUID(), title: "Harry Potter", type: "film")
let f2 = Film(id: UUID(), title: "Who am I", type: "film")
let f3 = Film(id: UUID(), title: "Scorpion", type: "series")
let f4 = Film(id: UUID(), title: "Manifest", type: "series")
let films = [f1, f2, f3, f4]
NavigationView {
List (films) { watch in
Text(watch.title)
}
.navigationBarItems(trailing: EditButton())
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct Film: Identifiable {
var id: UUID
var title: String
var type: String
}