![](/img/trans.png)
[英]RxSwift doesn't push back data from one ViewController to another
[英]Data isn't being passed from one viewcontroller to another
當我嘗試將某些變量從一個VC傳遞給另一個VC時,我要傳遞給VC的變量不會更新。 到目前為止,這是我的操作方式:
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
在彈出VC 2的函數中,我傳遞來自那個VC 2的數據以更新VC 1,如下所示:
func passData()
{
vcOne.note = self.note
vcOne.reward = self.reward
vcOne.deadline = self.deadline
navigationController?.popViewController(animated: true)
}
但是,當我通過后在VC 1中打印值時,它們仍然為空或0。
我以前曾經使用過這種方法來傳遞數據,但是這次似乎不起作用。 我應該做不同的事情嗎? 像我展示的那樣傳遞數據的最佳方法是什么?
編輯:這是我的真實代碼
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)
}
當我在VC 2中打印時,它們顯示正確的值,但是如果我在VC 2中執行該功能后嘗試在VC 1中打印,它們只會打印默認值。
在VC2中,使用var vcOne = VcOne()
創建一個新的VC1。 當傳遞數據時,您可能會將其傳遞給錯誤的實例。
您應該將屬性定義為:
var vcOne: VC1
並傳遞現有實例
更新資料
您必須設置對任務VC的引用:
func handleSetLocation()
{
let createMapController = CreateMapController()
createMapController.createMissionController = self
navigationController?.pushViewController(createMapController,animated: true)
}
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)
}
正如我的評論中提到的那樣,您正在重新分配一個新的vcOne,因此您設置了另一個對象並且不獲取數據。 通過將vc2.vcOne
分配給self,您就是在告訴vc2.vcOne
使用當前對象
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.