[Swift] How to reflect the array changed at the transition destination to the transition source and display it on Label etc.

Introduction

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.

Development environment

macOS 11.0.1
Xcode version 12.2
Swift version 5.3.1

code

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

Summary

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

[Swift] How to reflect the array changed at the transition destination to the transition source and display it on Label etc.
[Swift5] How to get an array and the complement of arrays
[Java] Memo on how to write the source
[Java] How to get the URL of the transition source
How to transition from the [Swift5] app to the iPhone settings screen
How to run React and Rails on the same server
[Swift] How to dynamically change the height of the toolbar on the keyboard
How to display 0 on the left side of the standard input value
How to install and configure the monitoring tool "Graphite" on Ubuntu
How to display products by category on the same list screen
How to place and share SwiftLint config files on the server
[Android] How to turn the Notification panel on and off using StatusBarManager
[Swing] How to display an arbitrary name on the menu bar on Mac
[Java] How to get the current date and time and specify the display format
How to switch the display of the header menu for each transition page
[Docker] How to build when the source code is bind-mounted on the container
How to constrain the action of the transition destination when not logged in