繁体   English   中英

TextField返回Nil

[英]TextField returning Nil

我在UICollectionView具有自定义UITextField ,即使我从中传递了一些值, textfield始终返回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)

自定义单元格类

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方法中访问它?

如果您已在视图控制器中初始化txtFldUserIdtxtFldPassword ,则从cellForItemAt方法中删除let ,例如

txtFldUserId = cell.txtFld
txtFldPassword = cell.txtFld

[我假设您已经在viewController中进行了初始化

var txtFldUserId: UITextField?
var txtFldPassword: UITextField?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM