如何在UICollectionViewCell中获取textfield的值

时间:2018-02-20 00:43:34

标签: ios swift uicollectionviewcell swift4

我有两节课。每当我得到文本字段的值时,它都不会打印任何内容,如果我摆脱了解包符号,它会打印出可选的(“”)。我该如何解决这个问题?

这是我的代码:

class userRegistration: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    // in faction cell for item at index path i have this code
    function collectionView(..){
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellid", for: indexPath) as! registerNewUser

        cell.registerButton.addTarget(self, action: #selector(click), for: .touchUpInside)

        return cell
    }

    @objc func click()        
    {
        let object = registerNewUser()
        let email = object.newEmailTextField.text!

        print(email)
    }
}

这是以编程方式在Swift 4中完成的。

1 个答案:

答案 0 :(得分:0)

您必须Indexpath item点击Register Button

@objc func click(sender: UIButton)        
{

    let cellPosition = sender.convert(CGPoint.zero, to: collexnVw)
    let indPath : IndexPath = collexnVw.indexPathForItem(at: cellPosition!)!

    let cell = collxnVw.cellForItem(at: indPath) as! registerNewUser 

    let email = cell.newEmailTextField.text!

    print(email)
}

您可以从此link

获得一些参考