-2

I have a two class. Whenever I get the value of the textfield it prints nothing and if I get rid of unwrap symbol it prints optional(""). How can I solve this?

This is my code:

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)
    }
}

This is done programmatically and in Swift 4.

joe
  • 1
  • 3
  • Your question is so far from what you need to do that I'm not sure how to help you. What is `registerNewUser()`. Is that an initializer of a class? Is that an instance method? How is that supposed to be getting access to a text field in your cell? You should probably implement the collection view delegate method `didSelectItemAtIndexPath` instead of trying to figure out which cell was tapped and get it's indexPath. – Duncan C Feb 20 '18 at 01:47
  • registerNewUser is a class – joe Feb 20 '18 at 02:33
  • Ok, so the construct `registerNewUser()` creates a brand new instance of that class. (Class names should start with an upper-case letter BTW.) How could this newly created object possibly contain a text field with your input in it? – Duncan C Feb 20 '18 at 02:40
  • thank you for correcting me for the naming of class im just new in swift. thats my problem i cant get the value is their any ways that i can get the data from registerNewUser class – joe Feb 20 '18 at 02:45
  • The expression `registerNewUser()` is creating a new instance of your `registerNewUser` (which should be `RegisterNewUser`) class. Instead, you need to save your `registerNewUser` object in an instance variable in your view controller somewhere. You are making so many mistakes I can tell you are trying to do too much too soon. You need to back off a bit and do some reading. – Duncan C Feb 20 '18 at 13:23

1 Answers1

0

You have to take Indexpath which item you are clicking 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)
}

You may get some reference from this link

McDonal_11
  • 3,935
  • 6
  • 24
  • 55