This time I would like to record how to pass the Label value in the collectionViewCell selected by tapping to the TextField of the transition destination in the transition using segue. By the way, the screen transition is done using NavigationController and segue.
macOS 11.0.1
Xcode version 12.2
Swift version 5.3.1
First, before writing the code to pass the value, [a] [i] [a] [e] [o] are assigned to the labels of each of the five cells in the following state. .. I would like to write a code that passes the contents of the selected cell to the transition destination, such as [A] when pressing the [A] cell and [I] when pressing the [I] cell.
FirstViewContoroller
import UIKit
class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource{
@IBOutlet weak var collectionView: UICollectionView!
var array = ["Ah","I","U","e","O"] //The number of arrays this time is 5
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array.count //Set the number of cells to the number of arrays
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for:indexPath)
//Put the String value of array in Label in the tagged Cell
var arrayLabel = cell.contentView.viewWithTag(1) as! UILabel
arrayLabel.text = array[indexPath.row]
return cell
}
}
FirstViewController
//Receive the selected label value
var selectedArray:String?
//Write the behavior when cell is selected with didSelectItemAt
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//The label in the selected Cell is assigned to selectedArray
selectedArray = array[indexPath.row]
//Screen transition
performSegue(withIdentifier: "toNext", sender: nil)
}
//Preparing segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let nextVC = segue.destination as? NextViewController
//Pass the transition source selectedArray to the transition destination selectedLabel
nextVC?.selectedLabel = selectedArray
}
NextViewController
import UIKit
class NextViewController: UIViewController {
@IBOutlet weak var textFiled: UITextField!
//The value selected in the transition source comes in
var selectedLabel:String?
override func viewDidLoad() {
super.viewDidLoad()
//Pass the passed value to TextFiled
textFiled.text = selectedLabel
}
By writing in this way, the label value in the selected cell can be passed to the transition destination.
In NextViewController, ** var selectedLabel: String? ** is set as an instance variable, and the value of the transition source is passed to it. However, you may think that you can pass the value directly to textFiled instead. I was the same. This method is wrong. When I actually write it, build it, and select a cell, I get the error ** Unexpectedly found nil while implicitly unwrapping an Optional value **. Simply put, there is an unexpected nil! is what it means. To put it simply, nil means empty. This is probably because the instance of the UITextFiled part of the transition destination does not exist when trying to pass the acquired value of the transition source to the textFiled of the transition destination.
https://capibara1969.com/1060/
This time it was about how to pass the value at the time of screen transition, but I will update what I learned from time to time! If you find it helpful, please use LGTM! !!
Recommended Posts