【问题标题】:Problems with getting text from UIAlertView textfield从 UIAlertView 文本字段获取文本的问题
【发布时间】:2015-05-07 19:00:59
【问题描述】:

在我的应用程序中,我想要一个带有文本字段的警报。单击“完成”后,我想将文本字段输入保存在字符串中。单击“取消”后,我只想关闭警报。我创建了这样的警报:

    var alert = UIAlertView()
    alert.title = "Enter Input"
    alert.addButtonWithTitle("Done")
    alert.alertViewStyle = UIAlertViewStyle.PlainTextInput
    alert.addButtonWithTitle("Cancel")
    alert.show()

    let textField = alert.textFieldAtIndex(0)
    textField!.placeholder = "Enter an Item"
    println(textField!.text)

警报如下所示:

我想知道如何从文本字段中获取文本,以及如何为“完成”按钮和“取消”按钮创建事件。

【问题讨论】:

  • 您需要实现适当的UIAlertViewDelegate 方法(并设置警报视图的delegate 属性)。
  • 谁能给我一个“UIAlerrViewDelegate”的例子?
  • 请发little searching
  • 我搜索了这个主题,现在我有办法检查按下了哪个按钮。但我还没有找到从文本字段中获取文本的运行解决方案。所以请帮我解决这个问题!

标签: ios swift alert uialertview textfield


【解决方案1】:

您可以使用 UIAlertController 而不是 UIAlertView。

我已经使用 UIAlertController 实现并测试了你真正想要的东西。请尝试以下代码

    var tField: UITextField!

    func configurationTextField(textField: UITextField!)
    {
        print("generating the TextField")
        textField.placeholder = "Enter an item"
        tField = textField
    }

    func handleCancel(alertView: UIAlertAction!)
    {
        print("Cancelled !!")
    }

    var alert = UIAlertController(title: "Enter Input", message: "", preferredStyle: .Alert)

    alert.addTextFieldWithConfigurationHandler(configurationTextField)
    alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler:handleCancel))
    alert.addAction(UIAlertAction(title: "Done", style: .Default, handler:{ (UIAlertAction) in
        print("Done !!")

        print("Item : \(self.tField.text)")
    }))
    self.presentViewController(alert, animated: true, completion: {
        print("completion block")
    })

【讨论】:

  • 正是我要搜索的内容。
  • 谢谢你.. :)
  • @DharmeshKheni 很高兴它帮助了你!
  • 很好的代码正是在寻找这个..:)
【解决方案2】:

你必须实现 UIAlertViewDelegate 的

optional func alertView(_ alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int)

【讨论】:

    【解决方案3】:

    对于 SWIFT 3

    @IBAction func ForgotPassword(_ sender: Any) {
    
        let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .alert)
    
        let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
            if let field = alertController.textFields![0] as? UITextField {
    
                // store and use entered data
    
            } else {
    
                print("please enter email id")
            }
        }
    
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
    
        alertController.addTextField { (textField) in
            textField.placeholder = "Email"
        }
    
        alertController.addAction(confirmAction)
        alertController.addAction(cancelAction)
    
        self.present(alertController, animated: true, completion: nil)
    
    }
    

    我希望它会帮助别人:)

    【讨论】:

      【解决方案4】:

      对于 iOS 8+,您应该使用 UIAlertController 而不是 UIAlertView。要支持 iOS 7,您应该实现 (UIAlertViewDelegate):

      func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int)
      {
          //...
          let textField = alertView.textFieldAtIndex(0)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-26
        • 1970-01-01
        • 2012-06-24
        • 1970-01-01
        • 2019-07-12
        相关资源
        最近更新 更多