在 swift 中循环通过 uitextfield class 时无法获得带有标签的正确文本字段?
unable to get proper textfield with tag while looping through uitextfield class in swift?
for subviews in self.profilescroll.subviews
{
if ((subviews is UITextField) && (subviews.tag == 0))
{
let btntext : UITextField = subviews as! UITextField
btntext.resignFirstResponder()
}
if ((subviews is UITextField) && (subviews.tag == 1))
{
let btntext : UITextField = subviews as! UITextField
btntext.resignFirstResponder()
}
}
这是我在文本字段中放置标签的地方。
for i in 0 ..< 2
{
btnbudget = UITextField()
btnbudget.frame = CGRect(x: (CGFloat(i) * (((screenWidth - 160)/2) + 40)) + 40, y: 0, width: (screenWidth - 160)/2, height: 50)
btnbudget.font = UIFont(name: "Metropolis-SemiBold", size: 18)
btnbudget.layer.cornerRadius = 10.0
btnbudget.layer.borderWidth = 1.0
btnbudget.layer.borderColor = UIColor(hexString:"4B45A0").cgColor
btnbudget.tag = i
btnbudget.backgroundColor = UIColor(hexString: "#00000000")
btnbudget.text = budgetlist.object(at: i) as? String
btnbudget.textColor = UIColor(hexString:"4B45A0")
btnbudget.textAlignment = .center
btnbudget.delegate = self
budgetbtnview.addSubview(btnbudget)
}
其中 profilescroll 是具有 3 个文本字段的视图,我需要在其中找到标签值范围为 0 到 1 的文本字段。但出于某种原因,我没有找到正确的文本字段,因此键盘没有返回。
您已经为文本字段分配了标签号,不需要遍历所有子视图。
textField.tag = 101
您可以直接使用标签号获取如下视图...
if let txtField = self.view.viewWithTag(101) as? UITextField {
print(txtField.text)
}
确保您给查看的所有标签都应该是唯一的,否则您可能会看到不需要的视图。
给定的标签应该是唯一的...试试这个..
for i in 100 ..< 102
{
btnbudget = UITextField()
btnbudget.frame = CGRect(x: (CGFloat(i) * (((screenWidth - 160)/2) + 40)) + 40, y: 0, width: (screenWidth - 160)/2, height: 50)
btnbudget.font = UIFont(name: "Metropolis-SemiBold", size: 18)
btnbudget.layer.cornerRadius = 10.0
btnbudget.layer.borderWidth = 1.0
btnbudget.layer.borderColor = UIColor(hexString:"4B45A0").cgColor
btnbudget.tag = i
btnbudget.backgroundColor = UIColor(hexString: "#00000000")
btnbudget.text = budgetlist.object(at: i) as? String
btnbudget.textColor = UIColor(hexString:"4B45A0")
btnbudget.textAlignment = .center
btnbudget.delegate = self
budgetbtnview.addSubview(btnbudget)
}
和....
for subviews in self.profilescroll.subviews
{
if ((subviews is UITextField) && (subviews.tag == 100))
{
let btntext : UITextField = subviews as! UITextField
btntext.resignFirstResponder()
}
if ((subviews is UITextField) && (subviews.tag == 101))
{
let btntext : UITextField = subviews as! UITextField
btntext.resignFirstResponder()
}
}
但是 self.view.viewWithTag(101) 方法比这个更合适
假设您有三个 UITextField
并且您已将 tags
设置为 0,1,2
现在,您可以简单地遍历 profilescroll
的所有 Subview
for case let textField as UITextField in self.profilescroll.subviews {
if textField.tag == 0 {
// do something
return
}
else if textfield.tag == 1 {
// do something
return
}
else if textfield.tag == 2 {
// do something
return
}
}
这也将忽略您的 subview
不是 UITextField
的情况,从而节省时间。
for subviews in self.profilescroll.subviews
{
if ((subviews is UITextField) && (subviews.tag == 0))
{
let btntext : UITextField = subviews as! UITextField
btntext.resignFirstResponder()
}
if ((subviews is UITextField) && (subviews.tag == 1))
{
let btntext : UITextField = subviews as! UITextField
btntext.resignFirstResponder()
}
}
这是我在文本字段中放置标签的地方。
for i in 0 ..< 2
{
btnbudget = UITextField()
btnbudget.frame = CGRect(x: (CGFloat(i) * (((screenWidth - 160)/2) + 40)) + 40, y: 0, width: (screenWidth - 160)/2, height: 50)
btnbudget.font = UIFont(name: "Metropolis-SemiBold", size: 18)
btnbudget.layer.cornerRadius = 10.0
btnbudget.layer.borderWidth = 1.0
btnbudget.layer.borderColor = UIColor(hexString:"4B45A0").cgColor
btnbudget.tag = i
btnbudget.backgroundColor = UIColor(hexString: "#00000000")
btnbudget.text = budgetlist.object(at: i) as? String
btnbudget.textColor = UIColor(hexString:"4B45A0")
btnbudget.textAlignment = .center
btnbudget.delegate = self
budgetbtnview.addSubview(btnbudget)
}
其中 profilescroll 是具有 3 个文本字段的视图,我需要在其中找到标签值范围为 0 到 1 的文本字段。但出于某种原因,我没有找到正确的文本字段,因此键盘没有返回。
您已经为文本字段分配了标签号,不需要遍历所有子视图。
textField.tag = 101
您可以直接使用标签号获取如下视图...
if let txtField = self.view.viewWithTag(101) as? UITextField {
print(txtField.text)
}
确保您给查看的所有标签都应该是唯一的,否则您可能会看到不需要的视图。
给定的标签应该是唯一的...试试这个..
for i in 100 ..< 102
{
btnbudget = UITextField()
btnbudget.frame = CGRect(x: (CGFloat(i) * (((screenWidth - 160)/2) + 40)) + 40, y: 0, width: (screenWidth - 160)/2, height: 50)
btnbudget.font = UIFont(name: "Metropolis-SemiBold", size: 18)
btnbudget.layer.cornerRadius = 10.0
btnbudget.layer.borderWidth = 1.0
btnbudget.layer.borderColor = UIColor(hexString:"4B45A0").cgColor
btnbudget.tag = i
btnbudget.backgroundColor = UIColor(hexString: "#00000000")
btnbudget.text = budgetlist.object(at: i) as? String
btnbudget.textColor = UIColor(hexString:"4B45A0")
btnbudget.textAlignment = .center
btnbudget.delegate = self
budgetbtnview.addSubview(btnbudget)
}
和....
for subviews in self.profilescroll.subviews
{
if ((subviews is UITextField) && (subviews.tag == 100))
{
let btntext : UITextField = subviews as! UITextField
btntext.resignFirstResponder()
}
if ((subviews is UITextField) && (subviews.tag == 101))
{
let btntext : UITextField = subviews as! UITextField
btntext.resignFirstResponder()
}
}
但是 self.view.viewWithTag(101) 方法比这个更合适
假设您有三个 UITextField
并且您已将 tags
设置为 0,1,2
现在,您可以简单地遍历 profilescroll
Subview
for case let textField as UITextField in self.profilescroll.subviews {
if textField.tag == 0 {
// do something
return
}
else if textfield.tag == 1 {
// do something
return
}
else if textfield.tag == 2 {
// do something
return
}
}
这也将忽略您的 subview
不是 UITextField
的情况,从而节省时间。