I'm trying to pass the address I get from the map into my other Viewcontroller and keep the same settings (i.e switch is still on) The mapVC is named MapViewController and the tableVC is named AddTaskTableViewController.
For your first question, you need to pass the data in prepareForSegue method, like this in your MapViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destination as? AddTaskTableViewController{
destinationViewController.address = self.address// Get address from map and stored here
}
}
For your second question about setting, you need to save setting into UseDefaults and whenever your view controller is loaded, set value from UserDefault. Here are example code
UserDefaults.standard.set("address", forKey: "address")
UserDefaults.standard.string(forKey: "address")
Related
I have 2 table view controllers - One is a registration input form the other a registration list form - and a model which is a computed property on the registration input form.
Upon cell selection, how do I need to update the registration forms cells with the data of the selected registration using a segue?
Thanks in advance.
It sounds like you want to use the UIViewController prepare(for:sender:) instance method.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "formSegue" {
let destinationViewController = segue.destination as? DestinationViewController
destinationViewController?.name = nameTextField.text
}
}
If you have multiple segues in your UIViewController then you need to give the segues identifiers in storyboard and check in the function which one was triggered, otherwise you don't need to check the segue.identifier. See Apple documentation here: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621490-prepareforsegue
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.
Explanation
In my app, there is the possibility to add members and to the members, which are displayed in a tableView. If the user selects a cell, he gets to a transactionsVCwhere all transactions of the user are displayed. The user can add new transactions to the member. I want to add the possibility, that a user can add a transaction to multiple members.
My idea
I want, that in the View where the user can add new transactions, that there is a way to get to a tableView where all members of the club are displayed.
Question
How can I display all members of the club?
I have this now:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let destination = segue.destination as? MultipleMemberTransactionsViewController
else {
return
}
destination.club = club
}
I think that I have to put something other than club in the line:
destination.club = club
This is what I got from Club to Members
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "memberViewController") as! MemberViewController
destinationVC = vc
let selectedRow = self.tableViewClub.indexPathForSelectedRow?.row
vc.club = clubs[selectedRow!]
}
self.navigationController?.pushViewController(destinationVC, animated: true)
}
What you think is the right way to do it. You have to create a new property for your members in your destination view controller, like this:
var members: (the type of your choice here certainly an array or dictionary) ?
And then inside the prepare(for:sender:) function pass the members to the destination view controller like this:
destination.members = yourMembers
And then use the list of members to populate a table view.
My app is split into 2 storyboards: a character selection one and a main application one. When the user selects a character the app segues's to the main application, and all the views of that storyboard should now relate to the character the user selected.
I'm trying to find out the best way to share a String that will have the selected character's information between all the main application storyboard's views. Right now I'm using UserDefaults to just set a global variable:
func loadMainApp(sender: UITapGestureRecognizer) {
let currentCharcter = allCharacters[(sender.view?.tag)!]
let defaults: UserDefaults = UserDefaults.standard
defaults.setValue(currentCharacter, forKey: "CurrentCharacter")
performSegue(withIdentifier: "MainAppSegue", sender: self)
}
From there all the view controllers in the Main App storyboard can fetch the string from UserDefaults.
Is this the best way of doing such a thing or is there a better way?
The better way is to pass the character to the viewController you are segueing to, and the easiest way to do that is with prepare(for segue. If you change your performSegue call to pass the sender on by saying performSegue(withIdentifier: "MainAppSegue", sender: sender) you will be able to access that in prepare(for like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//Safely check to make sure this is the correct segue by unwrapping the sender and checking the segue identifier
if let tapGesture = sender as? UITapGestureRecognizer, let tapGestureView = sender.view, let mainViewController = segue.destination as? MainViewController, segue.identifier == "MainAppSegue" {
//Get a reference to the character that was selected
let currentCharacter = allCharacters[tapGestureView.tag]
//Pass the character to the new viewController
mainViewController.character = currentCharacter
}
}
I made a couple assumptions about the name of the viewController you are performing the segue to, and assumed it has a variable called character where you can send your character. Your new viewController now has a reference to the character.
If I understand you well. Personally I am using Singeltons for achieving Global variable between Views rather than UserDefaults
class SomeClass{
static let sharedInstance = SomeClass()
var someString = "This String is same from any class"
}
Usage inside of some function :
SomeClass.sharedInstance.someString = "Changing Global String"
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" {
}