【问题标题】:Getting a String from UITextField in a UIAlertController (Swift)从 UIAlertController (Swift) 中的 UITextField 获取字符串
【发布时间】:2018-08-16 03:47:26
【问题描述】:

我这几天一直在拔头发,真的需要一些帮助。

我有一个这样定义的属性:

@objcMembers class Account: Object {

    dynamic var name = ""

当我用文字加载 .name 时,即,

            let newAccount = Account()
//            newAccount.name = self.accountTextField.text!
            newAccount.name = "John Brown"
            newAccount.isCurrent = true
            newAccount.highWaterBalance = 0.0

我明白了:

acct.name is John Brown
acct.highWaterBalance is 0.0
acct.isCurrent is true

但是,当我在self.accountTextField 中输入“John Brown”并使用以下代码时:

            let newAccount = Account()
            newAccount.name = self.accountTextField.text!
//            newAccount.name = "John Brown"
            newAccount.isCurrent = true
            newAccount.highWaterBalance = 0.0

我明白了:

acct.name is 
acct.highWaterBalance is 0.0
acct.isCurrent is true

我搜索了整个 SO,并认为我已经解决了 @rmaddy 对 this question 的贡献。

我认为我做得对,但我仍然从textField 得到一个空白。

任何帮助将不胜感激!

编辑 1:

这段代码:

        print("Johnny B Goode\n")
        print ("\(self.accountTextField.text!)")
        print("Johnny B Bad\n")

打印这个结果:

Johnny B Goode


Johnny B Bad

编辑 2:

为了清楚起见,也许我应该在之前提到这一点,这里是产生self.accountTextField.text 所在警报的IBAction 代码:

   @IBAction func addAccountButton(_ sender: Any) {

        // Pop alert to add new account



        let addAcctAlert = UIAlertController(title: "Create New Account", message: "Please name your new account", preferredStyle: UIAlertControllerStyle.alert)

        addAcctAlert.addTextField { (accountTextField) in
            accountTextField.placeholder = "Enter new account name"
            accountTextField.keyboardType = .`default`
        }

        addAcctAlert.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.destructive, handler: { action in

            // Create new Account with name, balance, isCurrent, save to disk
            print("Johnny B Goode\n")
            print ("\(self.accountTextField.text!)")
            print("Johnny B Bad\n")

            let newAccount = Account()
            newAccount.name = self.accountTextField.text!
//            newAccount.name = "John Brown"
            newAccount.isCurrent = true
            newAccount.highWaterBalance = 0.0

            self.addCreatedAccount(thisAccount: newAccount)
        }
        ))

        addAcctAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
        self.present(addAcctAlert, animated: true, completion: nil)
    }

确实看起来无论我在textField 中输入什么,它都不会产生任何结果。这可能吗?

【问题讨论】:

  • 我会验证 self.accountTextField 实际上是在返回一个值。如果不是,那么您需要查看情节提要绑定或控件的构建方式并将其添加到 UI
  • 检查正在编辑的文本字段是否为accountTextField。您可以通过在 textFieldDidChange 委托中打印值来轻松做到这一点。
  • 请参阅上面的编辑...
  • @rattletrap99 如果要通知他们,您需要标记他们。

标签: swift string uitextfield uialertcontroller


【解决方案1】:

如果您尝试从添加到 UIAlertController 的帐号文本字段中获取值,那么您应该这样做。

newAccount.name = addAcctAlert.textFields?.first?.text ?? "I haven't typed anything in alert controller's text field"

if let accountName: String = addAcctAlert.textFields?.first?.text {
    newAccount.name = accountName
}

【讨论】:

    【解决方案2】:

    好的,感谢@Rakesha Shastri、@sinner 和 @MadProgrammer 的慷慨指导,我在this question 中得到了第二个答案,它——经过一些微小的调整——完美地为我服务!

    非常感谢@Johnykutty 和@Andrey Gordeev 提供了简洁的解决方案!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      相关资源
      最近更新 更多