gpt4 book ai didi

ios - 数据没有从一个 View Controller 传递到另一个

转载 作者:行者123 更新时间:2023-11-28 12:17:58 25 4
gpt4 key购买 nike

当我尝试将一些变量从一个 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 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 中打印,它们只会打印默认值。

最佳答案

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 使用您当前的对象

关于ios - 数据没有从一个 View Controller 传递到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45819694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com