Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从UIAlertController(Swift)中的UITextField获取字符串_Swift_String_Uitextfield_Uialertcontroller - Fatal编程技术网

从UIAlertController(Swift)中的UITextField获取字符串

从UIAlertController(Swift)中的UITextField获取字符串,swift,string,uitextfield,uialertcontroller,Swift,String,Uitextfield,Uialertcontroller,我已经拔头发好几天了,真的需要一些帮助 我有一个定义如下的属性: @objcMembers class Account: Object { dynamic var name = "" 当我加载带有文字的.name时,即 let newAccount = Account() // newAccount.name = self.accountTextField.text! newAccount.name = "John

我已经拔头发好几天了,真的需要一些帮助

我有一个定义如下的属性:

@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
acct.name is 
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 John Brown
acct.highWaterBalance is 0.0
acct.isCurrent is true
acct.name is 
acct.highWaterBalance is 0.0
acct.isCurrent is true
我已经搜索了这么多,并且认为@rmaddy的贡献对我来说是有帮助的

我认为我做得对,但我仍然从
文本字段中得到一个空白

任何帮助都将不胜感激

编辑1:

此代码:

        print("Johnny B Goode\n")
        print ("\(self.accountTextField.text!)")
        print("Johnny B Bad\n")
打印此结果:

Johnny B Goode


Johnny B Bad
编辑2:

为了清楚起见,也许我以前应该提到这一点,下面是生成警报的
iAction
代码,该警报位于
self.accountTextField.text
中:

   @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)
    }

事实上,无论我在
文本字段中键入什么,它都不会产生任何结果。这可能吗?

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

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


好的,多亏了@Rakesha Shastri、@sinner和@MadProgrammer的慷慨指导,我在年找到了第二个答案,这个答案——加上几个小小的调整——对我来说非常有用


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

我将验证
self.accountTextField
是否实际返回值。如果不是,则需要查看情节提要绑定或控件的构建和添加方式,检查正在编辑的文本字段是否为
accountTextField
。您可以通过在
textfielddichange
委托中打印值轻松完成此操作。请参阅上面的编辑…@Crattrap99如果要通知人员,您需要标记他们。