Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i navigate from UIAlertController to a new screen (swiftUI)

how can I navigate from UIAlertController(UIKit) to a new view(SwiftUi)
due the the reason the swiftui alert can’t use a textfield
I used UIKit(UIalertcontroller)
and I want to open a new screen after I click the submit button

I have tried:

    var passwordField: UITextField? = nil

    let alert = UIAlertController(title: "Admin Passcode", message: "Please enter an administrator password", preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: "Submit", style: .default, handler: { Action in
        if let passwordField = passwordField {
            if passwordField.text == Constants.AdminPwd {
                print("Wow😁")
                DeviceConfigurationView(activeView: true)
            } else {
             let alert2 = UIAlertController(title: "Bad Password", message:            "Incorrect password", preferredStyle: .alert)
                alert2.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil))
                rootContoller().present(alert2, animated: true, completion: nil)
            }
        }
    }))

    alert.addAction(UIAlertAction(title: "Cancel", style: .default) {
        action in
        alert.dismiss(animated: true)
    })

    alert.addTextField {
        (textField) in

        textField.placeholder = "Password"
        textField.isSecureTextEntry = true
        textField.becomeFirstResponder()
        passwordField = textField

    }
    rootContoller().present(alert, animated: true, completion: nil)
    
    func rootContoller()->UIViewController{
    guard let  screen = UIApplication.shared.connectedScenes.first as? UIWindowScene                            else {
        return .init()
    }
    
    guard let root = screen.windows.first?.rootViewController else {
        return .init()
    }
    return root
        }
    }

this is the view I want to navigate to:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import UIKit

struct DeviceConfigurationView: View {
    
    @State var activeView: Bool 
    
    var body: some View {
        NavigationView {
            DeviceView()
        }
        .navigationViewStyle(.stack)
    }
}




struct DeviceView: View {
    
    @Environment(\.presentationMode) var presentationMode
    

    
    @State var  ToggleIsOn: Bool = false
    @State  var isExpandAudioSampleRate: Bool = false
    @State  var isExpandVideoResolution: Bool = false
    @State  var isExpandVideoResolutionForLocalVideoFile: Bool = false
    @State private var textField: String = ""
  
    var body: some View  {
            List {
                
                // Device
                Section(header:Text("Device"),
                        footer:
                            HStack {
                    Text("Device ID")
                    TextField("Device id", text: $textField)
                }) {
                    
                }
                
                // offline
                Section {
                    Toggle(isOn: $ToggleIsOn) {
                        Text("Offline Mode")
                            .opacity(5)
                    }
                    
                } header: {
                    Text("Offline")
                }
                
                // manage Recordings
                Section {
                    Button {
                        // action
                    } label: {
                        Text("Delete All Recordings")
                            .foregroundColor(Color.Theme.redcoolor)
                    }
                    
                } header: {
                    Text("Manage Recordings")
                }
                
                // Firebase
                Section {
                    HStack {
                        Text("Crash testing")
                        Spacer()
                        Button {
                            // action
                        } label: {
                            Text("Crash")
                                .foregroundColor(Color.blue)
                        }
                    }
                } header: {
                    Text("Firebase")
                }
                
                Section {
                    HStack{
                        Text("Audio SampleRate")
                            .bold()
                        Spacer()
                        Button {
                            isExpandAudioSampleRate.toggle()
                        } label: {
                            Image(systemName: isExpandAudioSampleRate ?  "chevron.up" : "chevron.down")
                                .foregroundColor(Color.blue)
                                .font(.system(size: 30))
                        }
                    }
                }
                
                Section {
                    HStack{
                        Text("Video Resolution")
                            .bold()
                        Spacer()
                        Button {
                            isExpandVideoResolution.toggle()
                        } label: {
                            Image(systemName: isExpandVideoResolution ?  "chevron.up" : "chevron.down")
                                .foregroundColor(Color.blue)
                                .font(.system(size: 30))
                        }
                    }
                }
                
                
                Section {
                    HStack{
                        Text("Video Resolution For Local Video File")
                            .bold()
                        Spacer()
                        Button {
                            isExpandVideoResolutionForLocalVideoFile.toggle()
                        } label: {
                            Image(systemName: isExpandVideoResolutionForLocalVideoFile ?  "chevron.up" : "chevron.down")
                                .foregroundColor(Color.blue)
                                .font(.system(size: 30))
                        }
                    }
                }
                
            }
            .listStyle(PlainListStyle())
            
            .navigationTitle("Device Configuration")
            .navigationBarItems(
                leading: Button(action: {
                    presentationMode.wrappedValue.dismiss()
                    print("Cancelled")
                }, label: {
                    Text("Cancel")
                }),
                trailing: Button(action: {
//                    MainView()
                    print("Saved")
                }, label: {
                    Text("Save")
            }))
    }
}

and I am getting this error
my error

>Solution :

From your code logic it seems to be like

if passwordField.text == CuratorConstants.AdminPwd {
    print("Wow😁")
    let controller = UIHostingController(rootView: DeviceConfigurationView(activeView: true))
    rootContoller().present(controller, animated: true, completion: nil)
} else {
    ...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading