[swift] How to control the behavior when the back button of NavigationBar is pressed

Introduction

I would like to record an explanation of what to do if I want to make a specific movement when I press the back button on the navigation bar.

Development environment

macOS 11.0.1
Xcode version 12.2
Swift version 5.3.1

What you want to implement

This time I would like to implement passing the value entered in TextFiled of the child screen to the parent when the back button of the Navigation bar is pressed. Pressing the back button to move from the child to the parent screen can be rephrased as pressing the back button to bring up the parent screen. When this parent screen appears, WillShow of UINavigationControllerDelegate is called. I would like to use this to control the behavior when the back button is pressed.

ChildViewController


import UIKit

                                      //Added UINavigationControllerDelegate
class ChildViewController: UIViewController,UINavigationControllerDelegate{
  
    @IBOutlet weak var b: UITextField!  
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //Write that the delegate of NavigationController is delegated by ChildViewController
        navigationController?.delegate = self
        
    }      
        
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {

       //You can access the variables of ParentViewController which is the parent screen by writing the following
        if let controller = viewController as? ParentViewController{
       //Write what you want to process
       //I want to implement this time, a will pass the text input to b which is TextFiled to the variable of the parent screen
            controller.a = b.text!
        }

    }
}

the important thing is ** ・ UINavigationControllerDelegate when declaring class ・ NavigationController? .Delegate = self ・ Write what you want to do in willShow ** is!

Referenced site

Reference: When pressing the back button of navigationbar-I want to pass a value

Recommended Posts

[swift] How to control the behavior when the back button of NavigationBar is pressed
[Swift] Setting the behavior when the Share button is pressed
How to perform a specific process when the back button is pressed in Android Fragment
[Android] Easily control the event when the Back button is pressed in Fragment
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
When the hover of Eclipse is hard to see
[swift5] How to execute processing when tabBar is tapped
[Swift] How to get the document ID of Firebase
[Swift] Get the timing when the value of textField is changed
How to change BackgroundColor etc. of NavigationBar in Swift UI
[Swift] How to dynamically change the height of the toolbar on the keyboard
[Swift] How to display when the quiz app answers correctly [Beginner]
[Swift5] How to get an array and the complement of arrays
How to output the value when there is an array in the array
[Swift UI] How to get the startup status of the application [iOS]
How to write the view when Vue is introduced in Rails?
How to decorate the radio button of rails6 form_with (helper) with CSS
Change the processing when the button on RecyclerView is pressed for each list
How to determine the number of parallels
[Swift] How to implement the countdown function
[Docker] How to build when the source code is bind-mounted on the container
How to find the total number of pages when paging in Java
How to sort the List of SelectItem
How to constrain the action of the transition destination when not logged in
[Ruby] How to prevent errors when nil is included in the operation
[swift5] How to change the color of TabBar or the color of item of TabBar with code
[Swift] How to get the number of elements in an array (super basic)
How to get the ID of a user authenticated with Firebase in Swift
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
How to set when "The constructor Empty () is not visible" occurs in junit
How to find the cause of the Ruby error
[Rails] Button to return to the top of the page
Customize how to divide the contents of Recyclerview
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
How to add sound in the app (swift)
[Swift] How to link the app with Firebase
How to get today's day of the week
How to delete the database when recreating the application
Output of how to use the slice method
[Swift UI] How to disable ScrollsToTop of ScrollView
[Swift] How to implement the fade-in / out function
How to display the result of form input
[Java] How to get the authority of the folder
I want to go back to a specific VC by tapping the back button on the NavigationBar!
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Install multiple submit buttons in Rails View to get the value of the pressed button
How to change the process depending on the list pressed when there are multiple ListViews
[Swift] How to display the entered characters in Widget via UserDefaults when using WidgetKit
How to fix the problem that the upper half is cut off when using UITabBar
How to identify the cause when GC occurs frequently and CPU usage is high
Summary of how to use the proxy set in IE when connecting with Java
[Java] How to get the URL of the transition source
How to delete / update the list field of OneToMany
How far is the correct answer to divide the process?
How to write Scala from the perspective of Java
[Ruby] How to find the sum of each digit
How to install the root certificate of Centos7 (Cybertrust)
[Java] How to get the maximum value of HashMap
When the month of the date is acquired, the January shift
[Rails] How to change the column name of the table