所以我基本上尝试创建一个只在首次启动时弹出的基本登录屏幕。 此登录屏幕显示为警报。 现在我希望用户在用户名和密码字段下方选择他的“成绩”,只需通过pickerview选择
为了遵循我的想法,到目前为止我的代码是:
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.
}
}
使用此代码,它应如下所示:
所以回到我的问题:在用户名和密码文本字段下面,应该有另一个文本字段输入,您可以通过选择器视图选择(或者只有选择器视图,如果它更容易处理)
我该怎么做?
答案 0 :(得分:0)
我找到了一种方法:
首先,您需要创建一个var来保存TextField的引用:
var textFieldPicker: UITextField?
其次,构建textField:
loginController.addTextField { (textField) in
//Configure the attributes of the second text box.
textField.placeholder = "Picker"
// Implement PickerView delegates
let pickerView = UIPickerView()
pickerView.dataSource = self
pickerView.delegate = self
let toolbar = UIToolbar()
toolbar.barStyle = UIBarStyle.default
toolbar.isTranslucent = true
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(self.dismissPickerPressed))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
toolbar.setItems([spaceButton, doneButton], animated: false)
toolbar.isUserInteractionEnabled = true
toolbar.sizeToFit()
textField.inputAccessoryView = toolbar
textField.inputView = pickerView
// Save the textField to assign values
self.textFieldPicker = textField;
}
第三,实现PickerView的代表
要在AlertView的TextField中指定值,您必须使用:
self.textFieldPicker?.text = "Value"
无论如何,我建议你创建一个自定义视图来管理登录
答案 1 :(得分:0)
这里有一些进一步的消息: @DiegoQ建议使用自定义视图来管理登录
我做了一些研究,发现了一个合适的YouTube视频 https://www.youtube.com/watch?v=WIaRs2d6Xm0
几乎完成了重建此场景的所有事情 - 但失败了。
所以现在我的问题是 - 有没有办法像youtube视频的所有者那样做并实现我上面提到的功能?
非常期待任何形式的帮助