因此,我基本上正在尝试创建基本登录屏幕,该屏幕仅在首次启动时弹出。 此登录屏幕显示为警报。 现在我想要的用户名和密码字段下的用户选择了“等级”,只需通过pickerview如何在Alert中使用UIPickerView作为Textfield
选择它为了追随自己的想法,这是我到目前为止的代码:
import UIKit
class ViewController: UIViewController {
func loginAlert() {
let loginController = UIAlertController(title: "Login", message: "Please enter your credentials.", preferredStyle: .alert)
let actionLogin = UIAlertAction(title: "Login", style: .default) { (action:UIAlertAction) in
//This is called when the user presses the login button.
let textUser = loginController.textFields![0] as UITextField;
UserDefaults.standard.set(textUser.text, forKey: "Username")
//print("Username: \(UserDefaults.standard.value(forKey: "Username")!)")
let textPW = loginController.textFields![1] as UITextField
UserDefaults.standard.set(textPW.text, forKey: "Password")
//print("Password: \(UserDefaults.standard.value(forKey: "Password")!)")
}
loginController.addTextField { (textField) in
//Configure the attributes of the second text box.
textField.placeholder = "E-mail"
}
loginController.addTextField { (textField) in
//Configure the attributes of the second text box.
textField.placeholder = "Password"
textField.isSecureTextEntry = true
}
//Add the buttons
loginController.addAction(actionLogin)
//Present the alert controller
if let x = UserDefaults.standard.object(forKey: "Username") as? String {
if let y = UserDefaults.standard.object(forKey: "Password") as? String {
print("Username: \(UserDefaults.standard.value(forKey: "Username")!)")
print("Password: \(UserDefaults.standard.value(forKey: "Password")!)")
}
else {
self.present(loginController, animated: true)
}
}
else {
self.present(loginController, animated: true)
}
}
override func viewDidAppear(_ animated: Bool) {
// Do any additional setup after loading the view, typically from a nib.
loginAlert()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
有了这个代码,它应该是这样的:
所以回到我的问题:用户名和密码文本框下,应该有另一个文本框输入其中,你可以通过拾取选择erview(或只有一个pickerview,如果它更容易处理)
我该怎么做?
首先,我真的很感谢你的回答 - 但老实说,我couldn't't得到它的工作。 –
好的,我已经做了一个快速的项目,告诉你它是如何工作的。你可以下载它[https://github.com/DiegoQuimbo/ExamplePickerInsideAlert] – DiegoQ
我已经改变了我们的程序 –