【发布时间】: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