2017-02-22 50 views
0

因此,我基本上正在尝试创建基本登录屏幕,该屏幕仅在首次启动时弹出。 此登录屏幕显示为警报。 现在我想要的用户名和密码字段下的用户选择了“等级”,只需通过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. 
    } 

} 

有了这个代码,它应该是这样的:

enter image description here

所以回到我的问题:用户名和密码文本框下,应该有另一个文本框输入其中,你可以通过拾取选择erview(或只有一个pickerview,如果它更容易处理)

我该怎么做?

回答

0

我发现了一个办法做到这一点:

首先,你需要箱子一个VAR保存文本字段的参考:

var textFieldPicker: UITextField? 

其次,建立你的文本字段:

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的代表

在A的TextField中赋值lertView,你必须使用:

self.textFieldPicker?.text = "Value" 

反正我建议你创建一个自定义视图管理登录

+0

首先,我真的很感谢你的回答 - 但老实说,我couldn't't得到它的工作。 –

+0

好的,我已经做了一个快速的项目,告诉你它是如何工作的。你可以下载它[https://github.com/DiegoQuimbo/ExamplePickerInsideAlert] – DiegoQ

+0

我已经改变了我们的程序 –

0

这里有一些进一步的消息: @DiegoQ作出了自定义视图的建议管理登录

我做了一些研究,并找到一个合适的YouTube视频 https://www.youtube.com/watch?v=WIaRs2d6Xm0

做了几乎所有重建这种情况下 - 但没有成功。

所以我现在的问题是 - 有没有办法像YouTube视频的所有者那样做并实现我上面提到的功能?

真的很期待任何形式的帮助