简体   繁体   English

我如何从 UIAlertController 导航到新屏幕(swiftUI)

[英]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我如何从 UIAlertController(UIKit) 导航到新视图(SwiftUi),因为 swiftui 警报无法使用我使用 UIKit(UIalertcontroller) 的文本字段,并且我想在单击提交按钮后打开一个新屏幕

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:这是我要导航到的视图:

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我收到这个错误我的错误

From your code logic, it seems to be like从您的代码逻辑来看,它似乎是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM