collectionviewcell将数据传递给相关的collectionview

时间:2018-03-13 18:50:29

标签: ios swift uicollectionview

我正在使用collectionview。在这个collectionview中我有一个按钮,我有2个不同的单元格,它是2个不同的collectionviewcell。当我按下按钮时,我需要从第二个单元格中获取文本。第二个单元格是包含用户名,电子邮件和密码字段的寄存器单元。 这是代码片段。

这是LoginController

    class LoginController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {

        ******* I am taking of cell; If I do not do that then TextField returns nil *******
        var registerController: RegisterControllerCell?

        override func viewDidLoad() {
            super.viewDidLoad()
            UIApplication.shared.statusBarStyle = .lightContent
            view.backgroundColor = UIColor.rgb(61, 91, 151)

            ******* and here is making cell is accessible *******
            registerController = RegisterControllerCell()
            setupComponents()
        }

        final func setupComponents() -> Void {
          .... components set up here
        }

    ******* this is the collectionview that should hold cells in it *******
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.register(LoginControllerCell.self, forCellWithReuseIdentifier: loginCellId)
        view.register(RegisterControllerCell.self, forCellWithReuseIdentifier: registerCellId)
        view.translatesAutoresizingMaskIntoConstraints = false
        view.bounces = false
        view.showsHorizontalScrollIndicator = false
        view.isPagingEnabled = true
        view.layer.cornerRadius = 8
        view.layer.masksToBounds = true
        view.backgroundColor = .white
        view.delegate = self
        view.dataSource = self
        return view
    }()


    @objc func hideKeyboard(){
        self.view.endEditing(true)
    }


    ********* HERE IS PROBLEM STARTS... this is the button that should return text of textfield when clicking occurs. *********
    let rlButton: UIButton = {
        let btn = UIButton()
        btn.translatesAutoresizingMaskIntoConstraints = false

        btn.setTitle("LOGIN", for: .normal)
        btn.layer.cornerRadius = 5
        btn.layer.masksToBounds = true
        btn.backgroundColor = UIColor.brown

        btn.addTarget(self, action: #selector(handleClick), for: .touchUpInside)

        return btn
    }()

    @objc func handleClick(){

        switch buttonState {
        case .register?:
            registerUser()
            break

        case .login?:
            print("login")
            break

        case .none:
            print("none of them")
            break
        }

    }

    ********* here is should return the value *********
    func registerUser() -> Void {

        ******** this should return a text but it is nil every time ********
        let username = registerController?.username.text
        print(username)

    }


    // collectionView stuff
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }

    // let colors: [UIColor] = [.black, .green]
    let colors: [colorEnumerations] = [.white, .white]
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: loginCellId, for: indexPath) as? LoginControllerCell else {
            fatalError("unresolved cell id")
        }

        if indexPath.item == 1 {
            guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: registerCellId, for: indexPath) as? RegisterControllerCell else {
                fatalError("unresolved cell id")
            }
            return cell
        }


        cell.backgroundColor = colors[indexPath.item].value
        return cell
    }



        }

,这是RegisterCell

class RegisterControllerCell: BaseCell {

override func setupComponents() {

    registerComponents()
}

final func registerComponents() {
    .... components set up
}

let holder: UIView = {
    let vw = UIView()
    vw.backgroundColor = .white
    vw.translatesAutoresizingMaskIntoConstraints = false
    return vw
}()

let seperator: UIView = {
    let spr = UIView()
    spr.translatesAutoresizingMaskIntoConstraints = false
    spr.layer.borderWidth = 0.5
    spr.layer.borderColor = UIColor.gray.cgColor
    spr.backgroundColor = UIColor.gray
    return spr
}()

let seperator2: UIView = {
    let spr = UIView()
    spr.translatesAutoresizingMaskIntoConstraints = false
    spr.layer.borderWidth = 0.5
    spr.layer.borderColor = UIColor.gray.cgColor
    spr.backgroundColor = UIColor.gray
    return spr
}()

let seperator3: UIView = {
    let spr = UIView()
    spr.translatesAutoresizingMaskIntoConstraints = false
    spr.layer.borderWidth = 0.5
    spr.layer.borderColor = UIColor.gray.cgColor
    spr.backgroundColor = UIColor.gray
    return spr
}()

******** I need to get its and others texts ********
let username: UITextField = {
    let input = UITextField()
    input.translatesAutoresizingMaskIntoConstraints = false
    input.placeholder = "Username"
    return input
}()

let email: UITextField = {
    let input = UITextField()
    input.translatesAutoresizingMaskIntoConstraints = false
    input.placeholder = "Email"
    return input
}()

let password: UITextField = {
    let input = UITextField()
    input.translatesAutoresizingMaskIntoConstraints = false
    input.placeholder = "Password"
    input.isSecureTextEntry = true
    return input
}()
}

感谢您的帮助,我真的需要它。

有点不是:顺便说一下,Textfield不是零,没有东西是它的文本,每次都返回nil。

0 个答案:

没有答案