TextField返回Nil

时间:2018-05-10 08:15:52

标签: ios swift uicollectionview uitextfield

我在UITextField中有自定义UICollectionViewtextfield总是返回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)
    }

1 个答案:

答案 0 :(得分:0)

您已初始化

let txtFldUserId = cell.txtFld

let txtFldPassword = cell.txtFld

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

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

txtFldUserId = cell.txtFld
txtFldPassword = cell.txtFld

[我假设你已经在你的viewController中初始化了

var txtFldUserId: UITextField?
var txtFldPassword: UITextField?

因为你的loginAction方法没有出现编译错误]