How to add a TextField to Alert in SwiftUI?

前端 未结 9 1946
温柔的废话
温柔的废话 2021-02-03 21:33

Anyone an idea how to create an Alert in SwiftUI that contains a TextField?

\"sample_image\"

9条回答
  •  时光说笑
    2021-02-03 21:48

    func dialog(){
    
           let alertController = UIAlertController(title: "Contry", message: "Write contrt code here", preferredStyle: .alert)
    
            alertController.addTextField { (textField : UITextField!) -> Void in
                textField.placeholder = "Country code"
            }
    
            let saveAction = UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in
    
                let secondTextField = alertController.textFields![0] as UITextField
                print("county code : ",secondTextField)
    
            })
    
            let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil )
    
    
            alertController.addAction(saveAction)
            alertController.addAction(cancelAction)
    
            UIApplication.shared.windows.first?.rootViewController?.present(alertController, animated: true, completion: nil)
    
    
        }
    

    Usage

    Button(action: { self.dialog()})
     {
    Text("Button")
    .foregroundColor(.white).fontWeight(.bold)
     }
    

提交回复
热议问题