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