Ios 在swift中,如何在警报弹出窗口中的文本字段中创建下拉表视图选择

Ios 在swift中,如何在警报弹出窗口中的文本字段中创建下拉表视图选择,ios,swift,tableview,textfield,Ios,Swift,Tableview,Textfield,我希望我在这里使用了正确的术语,我非常新,正在制作我的第一个应用程序 我有一个警报,我有3个文本字段要向我的N实体添加新记录。我希望一个textfield(textField2)有一个下拉选项来选择填充textfield,然后填充数据库。我在主线程中找到了一个简单地在独立文本字段中创建下拉列表的示例,并尝试在下面合并该示例,但在运行tableViewAlert.register行时,我得到了一个零错误。看起来这段代码实际上并没有创建tableView的实例?关于我能做些什么来解决这个问题,或者告

我希望我在这里使用了正确的术语,我非常新,正在制作我的第一个应用程序

我有一个警报,我有3个文本字段要向我的N实体添加新记录。我希望一个textfield(textField2)有一个下拉选项来选择填充textfield,然后填充数据库。我在主线程中找到了一个简单地在独立文本字段中创建下拉列表的示例,并尝试在下面合并该示例,但在运行tableViewAlert.register行时,我得到了一个零错误。看起来这段代码实际上并没有创建tableView的实例?关于我能做些什么来解决这个问题,或者告诉我是否用正确的方法解决这个问题的任何建议

我创建IBOutlet的唯一经验是通过将脚本拖到编辑器中,从脚本创建连接。有了这段代码,就没有与故事板的交互,所以我觉得我一定是在某个地方遗漏了一步。但是由于所有的东西都是由代码创建的,我猜这是没有必要的

这些是我班上最好的:

@IBOutlet weak var tableViewAlert:UITableView!

var values = ["Goat","Chicken","Cow","Pig","Sheep"]
let cellReuseIdentifier = "cell"
那么我有这个函数

 @IBAction func createNewAnimal(_ sender: Any) {
    print ("Create New Animal")
   
    let alert = UIAlertController(title: "New Animal", message: "Enter New Animal", preferredStyle: .alert)
    
    
    alert.addTextField { (textField) in
        textField.placeholder = "Name of Animal or Tag#"
    }
    
    alert.addTextField { (textField2) in
        textField2.placeholder = "Animal Type"
    }
    
    let alertButton = UIAlertAction(title: "Save", style: .default) { (action) in
        
        //create tableview for the textfield - drop down
        let textField = alert.textFields![0]
        let textField2 = alert.textFields![1]
      

        
        
        self.tableViewAlert.register(UITableViewCell.self, forCellReuseIdentifier: self.cellReuseIdentifier)
        
        textField2.delegate = self
        self.tableViewAlert.delegate = self
        self.tableViewAlert.dataSource = self
        

        func textFieldActive() {
            self.tableViewAlert.isHidden = !self.tableViewAlert.isHidden
            print ("unhide tableview")
        }
        
        //I commented this out because I was getting an error here too
        // Manage tableView visibility via TouchDown in textField
        
        //textField2.addTarget(self, action: #selector(textFieldActive), for: UIControl.Event.touchDown)
        
        
        
        func textFieldChanged(_ sender: AnyObject) {
            self.tableViewAlert.isHidden = true
        }
       

        
        func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
         {
             guard let touch:UITouch = touches.first else
             {
                 return;
             }
            if touch.view != self.tableViewAlert
             {
                 textField2.endEditing(true)
                 self.tableViewAlert.isHidden = true
             }
         }
        

        
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            textField.resignFirstResponder()
            return true
        }

            
           // MARK: UITableViewDelegate
            func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                // Row selected, so set textField to relevant value, hide tableView
                // endEditing can trigger some other action according to requirements
                textField2.text = self.values[indexPath.row]
                tableView.isHidden = true
                textField2.endEditing(true)
            }

            func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
                return 0.0
            }

        
        
        
@IBAction func createNewAnimal(uu发件人:任意){
打印(“创建新动物”)
let alert=UIAlertController(标题:“新动物”,消息:“输入新动物”,首选样式:。警报)
alert.addTextField{(textField)位于
textField.placeholder=“动物或标记的名称#”
}
alert.addTextField{(textField2)位于
textField2.placeholder=“动物类型”
}
让alertButton=UIAlertAction(标题:“保存”,样式:。默认值){(操作)在
//为文本字段创建表格视图-下拉列表
让textField=alert.textFields![0]
让textField2=alert.textFields![1]
self.tableViewAlert.register(UITableViewCell.self,forCellReuseIdentifier:self.cellReuseIdentifier)
textField2.delegate=self
self.tableviewart.delegate=self
self.tableViewAlert.dataSource=self
func textFieldActive(){
self.tableViewAlert.isHidden=!self.tableViewAlert.isHidden
打印(“取消隐藏表格视图”)
}
//我把这个注释掉了,因为我在这里也遇到了一个错误
//通过在textField中触地来管理tableView的可见性
//textField2.addTarget(self,action:#选择器(textFieldActive),用于:UIControl.事件.触地)
func textfield已更改(\发送方:AnyObject){
self.tableviewart.ishiden=true
}
func touchsbegined(touchs:Set,带有事件:UIEvent?)
{
防护罩让触摸:UITouch=触摸。首先
{
返回;
}
如果touch.view!=self.tableviewart
{
textField2.endEditing(真)
self.tableviewart.ishiden=true
}
}
func textField应该返回(textField:UITextField)->Bool{
textField.resignFirstResponder()辞职
返回真值
}
//标记:UITableViewDelegate
func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
//选中行,因此将textField设置为相关值,隐藏tableView
//endEditing可以根据需要触发其他一些操作
textField2.text=self.values[indexPath.row]
tableView.ishiden=true
textField2.endEditing(真)
}
func tableView(u tableView:UITableView,heightForHeaderInSection:Int)->CGFloat{
返回0.0
}