0

我有一個簡單的UICollectionView有3個單元格。單元有它自己的類,所有3個單元都使用這個類。單元類只有一個UITextField即單元大小。我想知道如何能夠掌握在3個單元的每一箇中輸入的內容UITextFields並將其傳遞到我的SecondController中。我如何實現這一目標?感謝你們。代碼如下:Swift 3:如何從單個文本框中獲取不同collectionView中的文本?

import UIKit 

class FirstCell: BaseCell { 

    var secondController: SecondController? 

    let textField: UITextField = { 
     let tv = UITextField() 
     tv.textColor = .white 
     return tv 
    }() 

    override func setupViews() { 
     super.setupViews() 
     backgroundColor = .lightGray 

     addSubview(textField) 

     textField.frame = CGRect(x: 12, y: 0, width: self.frame.width, height: self.frame.height) 
    } 
} 

private let reuseIdentifier = "Cell" 

class SecondController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.collectionView?.backgroundColor = .white 
     self.collectionView?.alwaysBounceVertical = true 

     self.collectionView?.register(FirstCell.self, forCellWithReuseIdentifier: reuseIdentifier) 

     self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Print", style: .plain, target: self, action: #selector(handlePrint)) 
    } 

    func handlePrint() { 
     // print here... 
    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 3 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(width: view.frame.width, height: 50) 
    } 

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

     cell?.secondController = self 

     if indexPath.item == 0 { 
      cell?.textField.placeholder = "First Textfield" 
     } 

     if indexPath.item == 1 { 
      cell?.textField.placeholder = "Second Textfield" 
     } 

     if indexPath.item == 2 { 
      cell?.textField.placeholder = "ThirdTextfield" 
     } 

     return cell! 
    } 

} 

回答

0

創建協議

protocol MyCellTextFieldDelegate { 
    func didChangeTextTo(text: String) 
} 

然後

extension SecondController: MyCellTextFieldDelegate { 
    func didChangeTextTo(text: String) { 
     //doStuff 
    } 
} 

你的變化中

var secondController: SecondController? 

weak var delegate: MyCellTextFieldDelegate? 

,改變你的方法是像

override func setupViews() { 
    super.setupViews() 
    backgroundColor = .lightGray 
    addSubview(textField) 
    textField.delegate = self //this line 
    textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged) //and this line 
    textField.frame = CGRect(x: 12, y: 0, width: self.frame.width, height: self.frame.height) 
} 

和你的類

class FirstCell: BaseCell, UITextFieldDelegate { 
    //other code 

    private func textFieldDidChange() { 
     self.delegate?.didChangeTextTo(text: (self.textField.text ?? "")) 
    } 
} 

和內部cellForItemAt

cell?.delegate = self 

編輯:如果您想獲得UICollectionViewCell那打電話給這個,你應該加

var tag: Int = 0 

你的細胞內和內cellForItemAt

cell?.tag = indexPath.row 

,然後改變你的MyCellTextFieldDelegate FUNC不僅texttag以及通過。

相關問題