This question already has answers here:
Passing data between view controllers
(45 answers)
How do I pass a string or data object between two view controllers?
(2 answers)
Closed 4 years ago.
I want to have 3 View Controllers; A, B, and C. View Controller A will be where the final value gets displayed, or the Main View Controller. View Controller B and View Controller C will have simple Text Fields as inputs. The values you put into the Text Fields in View Controller B and C will be added together and shown in View Controller A. You will also need to have buttons to implement the action. How can this be done?
For instance, if the user inputs the number 2 in View Controller B and the number 3 in View Controller C text fields, then View Controller A will show the number 5.
There are multiple methods to do so
You can use delegate, notification centre etc.
try this reference
Send data back
Send Data Back
and it would be more helpful if you can share your code, what have you done so far.
First create the variable "numberOfCViewController" in ViewController B and create two variables called "numberOfCViewControllerInA" and "numberOfBViewControllerInA" in ViewController A
Then just create segues between all the controllers and simply add this function to pass the TextFields data:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "CViewControllerToBViewController"
{
let BViewController = segue.destination as! BViewController
BViewController.numberOfCViewController = numberTextField.text!
}
}
By doing that you pass the numberTextField.text! over to the ViewController B.
All you now have to do is repeat the same techniche in the other ViewControllers until youve reached C.
Then you just simply add the two numbers and assign the result to the Label in ViewController C.
Depends on what your using:
Segue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "ViewControllerBToViewControllerC" else {
return
}
guard let viewControllerB = segue.destination as? ViewControllerB else {
return
}
viewControllerB.passedValue = <insert passed value>
}
Navigation Controller:
guard let navigationController = navigationController else {
return
}
guard let viewControllerB = UIStoryboard(name: "\(ViewControllerB.self)", bundle: nil) as? ViewControllerB else {
return
}
viewControllerB.passedValue = <insert passed value>
navigationController.pushViewController(viewControllerB, animated: true)
Related
This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 2 years ago.
I have UITabViewController and two UIViewControllers. I use storyboards and segues. In the first VC.
I get data from Firebase and save it. Then I need to pass this data to another vc in order to change the text of UILabel in the second vc.
Thank you
func fetchCeloriesData() {
refWorkout = workout!.title
Database.database().reference().child("programs").child(refWorkout).child("calories").observeSingleEvent(of: .value) { (snapshot) in
let caloriesData = snapshot.value as! String
print(caloriesData)
// self.dietVC.BurnedLabel.text? = caloriesData
}
}
If you are using storyboards and segues, you can simply use
func prepare(for segue: UIStoryboardSegue,
sender: Any?)
you can identify segue there from storyboard and sender will be self.
and then
func performSegue(withIdentifier identifier: String,
sender: Any?)
Here you have to take identifier from storyboard which will be string. and on the event of moving to second ViewController call this perfromSegue function.
I have two view controllers (autoClaimViewController and reviewAutoViewController). In autoClaimViewController, I have a dictionary (reviewTableViewData) that is made up of a struct called claimData (it contains two string variables and a UIImage variable). When the user hits the "Review" button, I want the reviewTableViewData dictionary to be passed to the second view controller so that it's data can be displayed on a table view in the second view controller (reviewAutoViewController). How do I pass this dictionary to the other view controller?
Please make your answers understandable for a beginner - I'm still learning. I'm moving between view controllers using storyboard segues.
Thanks.
Additional Question: Will the images that I stored in the variables be passed when the dictionary is passed? In other words, do images work like Integers and Strings, where they can be passed between variables without an issue?
My code:
struct claimData {
var images = UIImage()
var imageTitle = String()
var relatedUnrelated = String()
}
class autoClaimViewController: UIViewController {
var reviewTableViewData = [claimData]()
#IBAction func reviewButton(_ sender: Any) {
//PASSES THE INFORMATION TO THE REVIEW VIEW CONTROLLER
//FIX THIS... IT NEEDS TO SEND THE reviewTableViewData ARRAY.
//MAKE A DICTIONARY IN THE REVIEW CONTROLLER THAT RECEIVES THE DICTIONARY FROM THIS VIEW CONTROLLER.
let vc2 = reviewAutoViewController()
//Find out how to transfer a dictionary from one view controller to another
}
I think your best bet is to set up a segue in your storyboards which takes you from 1st view to the second view. Here is a guide.
https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html
In both views have a place to store the information you want to pass. So create a var in the secon view to hold reviewTableViewData from the first view.
Then in the 1st view you call
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.destination is reviewAutoViewController
{
let vc = segue.destination as? TertiaryViewController
vc?.reviewTableViewData = reviewTableViewData
}
}
This get the data ready to be sent.
Then you perform the segue and the data should be passed for you. Perfomr the seugue with this func triggered by the button or whatever you use to tranisiton between views.
func performSegue(withIdentifier identifier: String,
sender: Any?){
enter code here
}
In the code below, I'm trying to use the variable controller to fill in the type of my destination segue at the bottom. But instead I get an error message that says controller is undeclared. It's declared right there on the second line! I can see it! I realize there are many alternatives to using segue identifiers but I'm more interested in learning why this doesn't work.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var controller: UIViewController
switch segue.identifier {
case "ASegue":
controller = AViewController()
case "BSegue":
controller = BViewController()
case "CSegue":
controller = CViewController()
default:
controller = AViewController()
}
if let vc = segue.destination as? controller { //Use of undeclared type 'controller'
...
}
}
A cast must be to a type (literal), not a variable.
And in any case you cannot conflate all your action into a single controller variable as you seem to want to do, exactly because they are different types.
So you would simply move the cast into each case of the switch, casting to the type that you expect for that segue's destination controller, and doing the appropriate work on that controller in that case:
switch segue.identifier {
case "ASegue":
if let controller = segue.destination as? AViewController {
// do stuff
}
case "BSegue":
if let controller = segue.destination as? BViewController {
// etc.
Perhaps a slightly cleaner way of expressing this is to forget the identifier and just go for the view controller class, if each possible segue has a different view controller class:
switch segue.destination {
case let controller as AViewController:
// populate controller
case let controller as BViewController:
// populate controller
// etc.
I am creating an application in which there are 6 view controller in storyboard. The thing is that data is shared between the default view controller and the first one ( say A and B) which i added. i am using the prepareforseque method for passing data. the problem started when i added two more view controller. lets say C and D i created two new swift files and changed the two view controller class name. i created a textbox and button in C and label in D. when i pressed the button, the value of the text field is not passing into the D view controller although i used the same methods and code which i used for A and B. do i have to do anything else when i want to pass data between two newly added view controller.
first viewcontroller in which when a button is pressed value 1 needed to be passed:
class PlaySelectMenu: UIViewController {
var value = Int()
#IBAction func twotofive(sender: AnyObject) {
value = 1
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let nextView : PlayGameView = segue.destinationViewController as! PlayGameView
nextView.x = value
}
}
the second view controller which receive the value and print it
import Foundation
import UIKit
class PlayGameView: UIViewController{
var x = Int()
override func viewDidLoad() {
super.viewDidLoad()
print(x)
}
}
here i have added both the view controller from the object library and not working with the default one which is present in storyboard by default. i dont know why these two viewcontroller are not working. please help.
Regards Dev
One solution would be to write the data out to NSUserDefaults and then read it back from NSUserDefaults in the other view controller. Probably not the proper or correct way to share data between two view controllers, but it's been a reliable work around for me.
Other than that, you'd need to share your code so that we can see what's occurring.
Can you post also the code in your controllers C & D. And also if you have copy/paste the code inside your first two controllers into the two others, are you sure that in your prepareForSegue method you have changed the name of the destination segue ?
Assuming you have created the segue in Storyboard:
All you need is to do is put all of needed updates in prepareForSegue because twotofive is called after prepareForSegue.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
value = 1
let nextView : PlayGameView = segue.destinationViewController as! PlayGameView
nextView.x = value
}
Since you have connected your segue from button click to view controller, when you press button segue is automatically called. Instead of connecting segue from button to VC, connect VC to VC. Then in button click method at the last add below line:
#IBAction func twotofive(sender: AnyObject) {
value = 1
self.performSegueWithIdentifier("<Name of the segue identifier>", sender: self)
}
This will call your prepareForSegue. If you are calling more then one VC using segue from a VC then you can use segue.identifier to check which VC was called as below
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "CVC" {
}
A segue in my app is being called multiple times, which is causing the view to continuously "load" until the app stops calling the segue function. It does this about 4 times until it's finished. After placing breakpoints in the code I noticed that the app is bouncing between these two functions:
if success {
self.performSegueWithIdentifier("startGame", sender: "user")
}
That is the action that triggers the segue (the user swipes something). It then goes to the next breakpoint:
if (segue.identifier == "startGame") {
let destinationNavigationController = segue.destinationViewController as! GameViewController
destinationNavigationController.user = self.users[self.currentUser]
}
The app goes back and forth between the two sections about 4 times. When I created the segue in my storyboard I made sure to wire the segue from the view controller itself (not a table/UI view) to the destination view.
What else can I do to fix this?
Thanks!
In storyboard, create a segue by holding control and dragging from the yellow icon of the 1st view controller to the 2nd view controller.
Give it an id, and the kind should be push if they are embedded in a navigation controller.
In your 1st view controller class:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "startGame" {
let viewController = segue.destinationViewController as! GameViewController
// this next view controller should have property that will store passed value
viewController.user = self.users[self.currentUser]
}
}
When you want to move to the next view controller:
performSegueWithIdentifier("startGame", sender: self)