gpt4 book ai didi

ios - 返回 Nil 的文本字段

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

我在 UICollectionView 中有自定义 UITextFieldtextfield 始终返回 null,即使我从中传递了一些值,并且应用程序崩溃了在按钮上单击

代码:

func accountCollectionView(_ collectionView: UICollectionView?, cellForItemAt indexPath: IndexPath?) -> UICollectionViewCell?
{
let cell : RBLoginCell = collectionView!.dequeueReusableCell(withReuseIdentifier: "RBLoginCell", for: indexPath!) as! RBLoginCell


cell.backgroundColor = UIColor.clear

switch indexPath!.row {
case 0:
let txtFldUserId = cell.txtFld
txtFldUserId?.delegate = self
txtFldUserId?.placeholder = "Email Address"
txtFldUserId?.tag = 1
txtFldUserId?.keyboardType = .emailAddress
txtFldUserId?.returnKeyType = .next
txtFldUserId?.isSecureTextEntry = false

break
case 1:
let txtFldPassword = cell.txtFld
txtFldPassword?.delegate = self
txtFldPassword?.placeholder = "Password"
txtFldPassword?.tag = 2
txtFldPassword?.returnKeyType = .done
txtFldPassword?.isSecureTextEntry = true

break
default:
break
}
cell.setNeedsLayout()
return cell
}

使用此方法,应用程序在检查字符计数时崩溃

    func loginAction(sender:UIButton!)
{

if (txtFldUserId?.text?.characters.count)! > 0 {
if (txtFldPassword?.text?.characters.count)! > 0 {
user?.emailAddress = txtFldUserId?.text
user?.password = txtFldPassword?.text
postLoginRequest(user)

Custom cell class

class RBLoginCell  : RBParentCell {
var txtFld : CustomTextField?
var separator : UIView?


override init(frame: CGRect) {
super.init(frame: frame)
txtFld = getTextField()
separator = getImageView()


}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func getLabel() -> UILabel? {
let _lblInfo = UILabel(frame: CGRect.zero)
_lblInfo.backgroundColor = UIColor.clear
_lblInfo.textAlignment = .left
_lblInfo.textColor = UIColor.white
_lblInfo.numberOfLines = 0
addSubview(_lblInfo)
return _lblInfo
}

func getTextField(_ frame: CGRect) -> CustomTextField? {
let textField = CustomTextField(frame: frame)
textField.borderStyle = .none
textField.placeholder = ""
textField.backgroundColor = UIColor.clear
textField.autocorrectionType = .no
// no auto correction support
textField.keyboardType = .default
// use the default type input method (entire keyboard)
textField.returnKeyType = .next
textField.enablesReturnKeyAutomatically = false
textField.clearButtonMode = .never
textField.text = ""
textField.contentVerticalAlignment = .center
textField.autocapitalizationType = .none
//textField.frame = CGRectMake(X, Y,Width,Height);
textField.alpha = 1.0
textField.isUserInteractionEnabled = true
let lblleft: UILabel? = getLabel()
lblleft?.frame = CGRect(x: 0, y: 20, width: self.frame.size.width, height: self.frame.size.height)
lblleft?.backgroundColor = UIColor.clear
lblleft?.text = "\n+91"
lblleft?.font = UIFont(name: Constants.HelveticaNeue, size: 14)//FONT_HelveticaNeue(14.0)
lblleft?.textColor = UIColor(hexString: "555555")
textField.leftView = lblleft
textField.leftViewMode = .never

addSubview(textField)
return textField
}

override func getImageView() -> UIImageView? {
let imageView = UIImageView(frame: CGRect.zero)
imageView.backgroundColor = UIColor.lightGray
addSubview(imageView)
return imageView
}

override func layoutSubviews() {
super.layoutSubviews()
txtFld?.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height-1)

separator?.frame = CGRect(x: 0, y: (txtFld?.frame.size.height)!, width: frame.size.width, height: 1)
}

最佳答案

您已初始化

let txtFldUserId = cell.txtFld

let txtFldPassword = cell.txtFld

在您的cellForItemAt方法中,如何在loginAction方法中访问它?

如果您已在 View Controller 中初始化了 txtFldUserIdtxtFldPassword,则从 cellForItemAt 方法中删除 let就像

txtFldUserId = cell.txtFld
txtFldPassword = cell.txtFld

[我假设您已在 viewController 中初始化

var txtFldUserId: UITextField?
var txtFldPassword: UITextField?

因为您在 loginAction 方法中没有收到编译器错误]

关于ios - 返回 Nil 的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50268463/

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