This time, we are using NavigationController to perform screen transitions. Therefore, we use willShow of navigationController which is called when returning from the transition destination to the transition source. In addition, Label is placed in Cell of collectionView, and the value of array (String) is displayed there.
macOS 11.0.1
Xcode version 12.2
Swift version 5.3.1
Give the variable a as a String type to the parent screen, and give 5 values that do not contain any value as initial values. This is given the previous value to the Label installed in the Cell using the collectionView. By setting the property observer when the array changed at the transition destination is passed to the transition source, it becomes possible to write the processing when the change occurs. Observers include willSet, which is called before the property change, and didSet, which is called after the property change. This time, we will use the ** didset ** that will be called after the change.
FirstViewContoroller
import UIKit
class ViewController:
//Since collectionView is used, write the following two
UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
//Write for the same reason as above
@IBOutlet weak var collectionView: UICollectionView!
var a = ["","","","",""] {didSet{
//By writing in didset, reloadData can be performed when the variable a is changed, and the displayed value in the label can also be changed.
collectionView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return a.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for:indexPath)
cell.backgroundColor = .lightGray
//The value of variable a is displayed in order on the tagged label.
var aLabel = cell.contentView.viewWithTag(1) as! UILabel
aLabel.text = a[indexPath.row]
return cell
}
//Besides, write code that transitions in some way
}
NextViewController
import UIKit
class NextViewController: UIViewController,UINavigationControllerDelegate {
@IBOutlet weak var textFiled: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
//The process called when the back button on the navigation bar is pressed. By writing the following, you can change the value of the transition source at the transition destination.
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
//Enter the file name of the parent screen
if let controller = viewController as? FirstViewContoroller{
//This time change the first value of variable a
//The character typed in the text field of the transition destination screen changes the first value of the transition source variable a.
controller.a[0] = textfiled.text!
}
The important thing in this code is ** didset **, which is one of the property observers. By writing ** collectionView.reloadData () ** in this, when the array that is a variable changes, ** collectionView.reloadData () ** is called, and the value displayed on the label in the cell can be updated every time it is changed.
Recommended Posts