I'm trying to access another SwiftUI View() from a function that my button calls, not directly from the button. I know that if I am to use a button I can simply do this
Button(action: {
self.showingDetail.toggle()
}) {
Text("Show Detail")
}.sheet(isPresented: $showingDetail) {
DetailView()
}
But, My button
Button(action: {
self.makeGetCall()
}) {
Text("LOGIN") .font(.largeTitle)
.fontWeight(.heavy)
.foregroundColor(Color.black)
}
.alert(isPresented: $showingAlert) {
Alert(title: Text("Logging Error"), message: Text("Invalid username or password."), dismissButton: .default(Text("OK.")))
}
Calls a function that needs to decide which view to go to..
So within makeGetCall(), how can I go to a new view?
Here's the current code for makeGetCall()
func makeGetCall() {
// Set up the URL request
let todoEndpoint: String = "https://balancingpawsdogtraining.com/api/user/generate_auth_cookie/?username=\(username)&password=\(password)"
guard let url = URL(string: todoEndpoint) else {
print("Error: cannot create URL")
return
}
let urlRequest = URLRequest(url: url)
// set up the session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET on /todos/1")
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
guard let todo = try JSONSerialization.jsonObject(with: responseData, options: [])
as? [String: Any] else {
print("error trying to convert data to JSON")
return
}
// now we have the todo
// let's just print it to prove we can access it
print("The todo is: " + todo.description)
// the todo object is a dictionary
// so we just access the title using the "title" key
// so check for a title and print it if we have one
if let status = todo["status"] as? String
{
if status == "error"
{
self.showingAlert = true
return
}
else
{
AppVariables.cookie = todo["cookie"] as! String
self.showLinkTarget = true
print("The title is \(todo["cookie"] as! String)")
MainScreenView()
}
}
} catch {
print("error trying to convert data to JSON")
return
}
}
task.resume()
}
I tried
AppVariables.cookie = todo["cookie"] as! String
self.showLinkTarget = true
print("The title is \(todo["cookie"] as! String)")
MainScreenView()
Calling just MainScreenView() --- this is the name of the View I'd like to go to in this situation, but that does nothing.
So how Can I turn that MainScreenView() into actually navigating to MainScreenView()
---- EDIT ----
I figured out I can just do this
}.sheet(isPresented: $showingDetail) {
MainScreenView()
}
and change the showingDetail in my called function.
However, this "Overlays" my current view with the new one, how do I completely switch to it?