Uzumaki Naruto
Uzumaki Naruto

Reputation: 547

Swift - Cannot pass user gesture from custom UICollectionViewCell to UITextField inside it

I have a custom UICollectionViewCell (let's call it MyCustomCell) that has a custom UITextField (let's call it MyCustomField) inside it that users can add text to. However, it seems no matter what I do, the tap inputs don't get from the MyCustomCell to its sub-view MyCustomField. Previously, I had this same arrangement, except MyCustomCell is a UITableViewCell, and that worked for some reason.

Right now I had to do a workaround, where whenever a cell is tapped, the cell sends a becomeFirstResponder() message to the textfield. However, this doesn't let the user tap on the "clear text" button inside the MyCustomField, nor does it let the user move the cursor around in the text field:

// function inside the MyCustomCell, called by the view controller
// whenever it thinks the user tapped on a cell:
func cellTapped()
{
    // this is an instance of MyCustomField:
    input.becomeFirstResponder();
}

Is there any way to "pass" the actual gesture to the MyCustomField (from either the MyCustomCell, or from the the view controller that manages them both)?

Update: this is what the view hierarchy looks like for the MyCustomCell (sub-class of UICollectionViewCell):

view hierarchy of MyCustomCell, with the MyCustomField in it

Upvotes: 1

Views: 95

Answers (1)

Uzumaki Naruto
Uzumaki Naruto

Reputation: 547

This may be of help to others who have similar issue: I believe @MultiColourPixel is correct in the comment above that there is a hidden "view" standing between the MyCustomField and MyCustomCell. What I did was:

  1. Backed up the .xib and .swift files for the code for MyCustomCell and MyCustomField.
  2. Delete the .xib and .swift files.
  3. From the XCode menu, re-create the .swift file -- and make sure you check the "create XIB" as well when you create the .swift file / class that is subclass of the UICollectionViewCell.
  4. Copy paste the old xib / swift contents into the new xib / swift.
  5. It should work, without needing to explicitly send becomeFirstResponder() to the MyCustomField.

Upvotes: 1

Related Questions