How to pass the value to another screen

Pass the value at the time of screen transition

By passing a value to the variable var of the transition destination at the time of screen transition, you can pass the value to another screen.

ViewController


let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "second") as! secondViewController
secondViewController. value = "value"
self.present(secondViewController, animated: true, completion: nil)

secondViewController


class secondViewController: UIViewController {

    var value = String()

     override func viewDidLoad() {
          super.viewDidLoad()
     }
}

Pass the value except at the time of screen transition

Because tabBarController creates a screen when you first switch tabs and does not delete it You need a way to pass a value other than at the time of screen transition.

You can pass a value or perform a process to another screen by using a protocol to execute it after a specific process.

ViewController


var process: anotherScreenProcess!

class ViewController: UIViewController {

     override func viewDidLoad() {
          super.viewDidLoad()

          self.process = secondViewController() as anotherScreenProcess
          self.process.sendValue()
     }
}

secondViewController



protocol anotherScreenProcess {
    func sendValue()
}

class secondViewController: UIViewController, anotherScreenProcess {
     override func viewDidLoad() {
          super.viewDidLoad()
     }

     //Processing from another screen
     func sendValue() {

     }
}

About tabBarController

tabBarController creates a screen only at the first screen transition and is not deleted. Therefore, by deleting the tabBarController created other than using the protocol, You can recreate the screen itself.

python


self.tabBarController!.viewControllers![1].dismiss(animated: true, completion: nil)

Recommended Posts

How to pass the value to another screen
[Swift] How to pass the Label value in the selected collectionViewCell to the destination TextField
9 Corresponds to the return value
How to return to the previous screen by Swipe operation
How to send value in HTML without screen transition
How to move to the details screen by clicking the image
[Java] How to get the maximum value of HashMap
[Android Studio] [Java] How to fix the screen vertically
[ruby] How to assign a value to a hash by referring to the value and key of another hash
How to transition from the [Swift5] app to the iPhone settings screen
How to retrieve the hash value in an array in Ruby
How to change the setting value of Springboot Hikari CP
How to use the link_to method
How to use the include? method
How to use the form_with method
Pass the i18n locale to JavaScript
How to find the average angle
[Rails] How to prevent screen transition
How to use the wrapper class
How to make a splash screen
How to pass Oracle Java Silver
How to add the delete function
How to output the value when there is an array in the array
[Swift5] How to communicate from ViewController to Model and pass a value
How to increment the value of Map in one line in Java
[Rails 5] How to display the password change screen when using devise
[Java] How to use the File class
How to delete the wrong migration file
[Java] How to use the hasNext function
How to put out the error bundling
[Java] How to use the HashMap class
How to delete the migration file NO FILE
[Rails] How to use the map method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to determine the number of parallels
How to output Java string to console screen
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
[Swift] How to implement the countdown function
How to change the timezone on Ubuntu
How the website is displayed on the screen
Ransack sort_link How to change the color!
[Processing × Java] How to use the class
How to sort the List of SelectItem
How to install the legacy version [Java]
How to get the date in java
[Swift5] How to implement standby screen using'PKHUD'
[Swift5] How to create a splash screen
[Processing × Java] How to use the function
[Struts] How to pass values between jsps
[Java] How to use the Calendar class
[Spring MVC] How to pass path variables
Summarized how to climb the programming stairs
How to output the sum of any three numbers, excluding the same value
How to return a value from Model to Controller using the [Swift5] protocol
How to get the value after "_" in Windows batch like Java -version
How to get the setting value (property value) from the database in Spring Framework
How to change the value of a variable at a breakpoint in intelliJ
Android development, how to check null in the value of JSON object
How to create your own annotation in Java and get the value