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以及通过。

相关问题