gpt4 book ai didi

ios - 仅当文本字段为空时如何显示图像?

转载 作者:行者123 更新时间:2023-11-30 12:31:25 25 4
gpt4 key购买 nike

我有 4 个文本字段、一个按钮和一个图像,我想要实现的是,当按下按钮时,它应该警告用户有空文本字段,并显示指向空文本字段的图像,但当用户将文本放入空文本字段时使图像消失。谢谢

这是我的功能

func checkFields(){
let userEmail = user_EmailTxtField.text!
let userPassword = user_PassTxtField.text!
let passwordConfirm = pass_ConfirmTxtField.text!
let userPhone = user_PhoneTxtField.text!
//Set backArrow image to show input error to password
let imageName = "backArrow.png"
let image = UIImage(named: imageName)
let passImageView1 = UIImageView(image: image!)
passImageView1.frame = CGRect(x: 319, y: 331, width: 49, height: 49)
view.addSubview(passImageView1)
//Show Arrow image to password confirm error
let passConfirmImageView1 = UIImageView(image: image!)
passConfirmImageView1.frame = CGRect(x: 319, y: 394, width: 49, height: 49)
view.addSubview(passConfirmImageView1)
//Show arrow Image to email error
let emailImageView1 = UIImageView(image: image!)
emailImageView1.frame = CGRect(x: 319, y: 270, width: 49, height: 49)
view.addSubview(emailImageView1)
//Set image to phonetxt field error
let phoneImageView1 = UIImageView(image: image!)
phoneImageView1.frame = CGRect(x: 319, y: 209, width: 49, height: 49)
view.addSubview(phoneImageView1)
if userPhone.isEmpty {
phoneImageView1.isHidden = false
} else {
phoneImageView1.isHidden = true
}
if userEmail.isEmpty {
emailImageView1.isHidden = false
}
if userPassword.isEmpty {
passImageView1.isHidden = false
}
if passwordConfirm.isEmpty {
passConfirmImageView1.isHidden = false
}
}

最佳答案

您可以调用一个函数来处理您的检查,然后如果文本不为空,则隐藏您的图像,如下所示:

1 将目标添加到您为事件创建文本字段的位置 .editingChanged:

user_EmailTxtField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
user_PhoneTxtField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
pass_ConfirmTxtField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
pass_ConfirmTxtField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)

2 设置用于检查文本字段是否被编辑的方法:

func textFieldDidChange() {
emailImageView1.isHidden = user_EmailTxtField.hasText
phoneImageView1.isHidden = user_PhoneTxtField.hasText
passImageView1.isHidden = pass_ConfirmTxtField.hasText
passConfirmImageView1.isHidden = pass_ConfirmTxtField.hasText
}

因此,如果您将此示例应用于所有文本字段,则不需要 checkFields() 中的检查,并且可以将方法重命名为 setupTextFields()例如仅将其用作文本字段的创建

在类的所有方法中使用 ImageView 的示例,但请务必在 viewDidLoad() 中进行初始化,以避免在解包可选值时出现发现 nil 错误

class YourClass: UIViewController {

// ImageView Reference
var emailImageView1: UIImageView!

// then your checkFields method
func checkFields() {
// code before
emailImageView1 = UIImageView(image: image!)
// code after
}
}

关于ios - 仅当文本字段为空时如何显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43506003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com