2017-06-18 77 views
1

我需要您的幫助來了解如何在點按按鈕時如何與UITextField進行交互。當在UICollectionView中點擊按鈕時,輸入UITextField

這是我在我的ViewController代碼:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
{ 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCollectionViewCell 

    cell.addBookTapped.addTarget(self,action: #selector(buttonTapped(_:)),for: .touchUpInside) 
    cell.configureCell(with: books[indexPath.row], indexCell: indexPath.row) 

    return cell 
} 

func buttonTapped(_ sender: UIButton) 
{ 
    let tag = sender.tag 
    //Do Stuff 
} 
MyCollectionViewCell

而且,我配置所有按鈕和textView,在我的按鈕添加標籤:

@IBOutlet weak var myTextFielThatIWantToFieldWhenButtonTapped: UITextField! 

在我ViewController在我的func buttonTapped我無法達到myTextFielThatIWantToFieldWhenButtonTapped

當點擊按鈕時,如何在其中寫入內容並直接在視圖中可見?

回答

1

你必須在像下面特定細胞的indexPath呼籲cellForItem得到您想要的細胞(例如你的細胞是在第0項0)

let indexPath = IndexPath(item: 0, section: 0) 
let cell = collectionView.cellForItem(at: indexPath) as? YourCustomCollectionViewCellClass 

,那麼你可以在裏面訪問的變量你的細胞類:

cell.myTextFielThatIWantToFieldWhenButtonTapped 
+0

完美,這工作完美:)。 – stix