Reputation: 751
When I try to pass some variables from one VC to the other, the variables in the VC i'm passing to, doesn't get updated. Here is how I've done it so far:
VC 1
var note: String = ""
var reward: Int = 0
var deadline: Int = 0
VC 2
var vcOne = VcOne()
var note: String = "Some note..."
var reward: Int = 100
var deadline: Int = 988934
In the function where I pop VC 2, I pass the data from that one to update VC 1 like this:
func passData()
{
vcOne.note = self.note
vcOne.reward = self.reward
vcOne.deadline = self.deadline
navigationController?.popViewController(animated: true)
}
But when I print the values in VC 1 after I have passed, they are still empty or 0.
I have used this method to pass data before, but this time it doesn't seem to work. Should I be doing it differently? What's the best way to pass data like the way I showed?
EDIT: Here is my real code
VC 1:
class CreateMissionController: UIViewController, UITextViewDelegate
{
var deadline: Int = 0
var missionLatitude: Double = 0
var missionLongitude: Double = 0
func handleSetLocation()
{
let createMapController = CreateMapController()
navigationController?.pushViewController(createMapController,animated: true)
}
VC 2
class CreateMapController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate
{
var createMissionController: CreateMissionController?
var missionLatitude: Double?
var missionLongitude: Double?
var deadline: Int?
func handleChooseLocation()
{
missionLatitude = missionLocation!.latitude
missionLongitude = missionLocation!.longitude
createMissionController?.missionLatitude = missionLatitude!
createMissionController?.missionLongitude = missionLongitude!
let timeInterval = deadlineDate?.timeIntervalSince1970
let deadlineInt = Int(timeInterval!)
createMissionController?.deadline = deadlineInt
print(missionLatitude!)
print(missionLongitude!)
print(deadlineInt)
navigationController?.popViewController(animated: true)
}
when I print in VC 2, they display the correct values, but if I try to print in VC 1 after I have executed the function in VC 2, they just print the default values.
Upvotes: 0
Views: 60
Reputation: 21137
In VC2 you create a new VC1 with var vcOne = VcOne()
. When passign your data, you are probably passing it to the wrong instance.
You should define the property as:
var vcOne: VC1
and pass the existing instance instead
Update
You have to set a reference to the mission VC:
func handleSetLocation()
{
let createMapController = CreateMapController()
createMapController.createMissionController = self
navigationController?.pushViewController(createMapController,animated: true)
}
Upvotes: 1
Reputation: 295
VC1
var note: String = ""
var reward: Int = 0
var deadline: Int = 0
var vc2 = VcTwo()
vc2.vcOne = self
VC 2
var vcOne : VcOne
var note: String = "Some note..."
var reward: Int = 100
var deadline: Int = 988934
func passData()
{
vcOne.note = self.note
vcOne.reward = self.reward
vcOne.deadline = self.deadline
navigationController?.popViewController(animated: true)
}
As mentionned in my commentary, you're reassigning a new vcOne, so you set another object and don't get the data back.
by assigning vc2.vcOne
to self, you're telling vc2 to use your current object
Upvotes: 1