在Swift中更新`UserDefaults`中的值的问题(Issue in updating value from `UserDefaults` in Swift)

编程入门 行业动态 更新时间:2024-10-28 04:20:06
在Swift中更新`UserDefaults`中的值的问题(Issue in updating value from `UserDefaults` in Swift)

我通过UserDefaults从我的UserDefaults发送字符串值到UserDefaults 。 但是当我通过后退按钮从vc2移回到vc1时,该值不会更新。 我在viewWillAppear方法中获得了vc1的价值。 但我的价值不会更新。 我通过推送方法从vc1导航到vc2。

这就是我如何将用户默认值存储在vc2中,

cartItems = cartItems + 1 print(cartItems) let badgeCount = String(cartItems) print(badgeCount) let rightBarButton = self.navigationItem.rightBarButtonItem let badge = String(badgeCount) rightBarButton?.addBadge(text: badge) UserDefaults.standard.set(badgeCount, forKey: "cartsItems") UserDefaults.standard.synchronize()

并在vc1我得到这样的viewWillAppear委托,

let count = UserDefaults.standard.string(forKey: "cartsItems") print(count)

当我从vc2通过后退按钮值回到vc1时,永远不会更新,当我调用某个其他vc并再次调用vc1时,它会得到更新。 我当时如何更新价值?

I'm sending a string value from my vc2 to vc1 through UserDefaults. But when I move back to vc1 from vc2 through back button the value doesn't update. I'm getting value in vc1 in viewWillAppear method. But my value does not update. I navigate from vc1 to vc2 through push method.

This is how i stored the value in user default in vc2,

cartItems = cartItems + 1 print(cartItems) let badgeCount = String(cartItems) print(badgeCount) let rightBarButton = self.navigationItem.rightBarButtonItem let badge = String(badgeCount) rightBarButton?.addBadge(text: badge) UserDefaults.standard.set(badgeCount, forKey: "cartsItems") UserDefaults.standard.synchronize()

and in vc1 i get like this in viewWillAppear delegate,

let count = UserDefaults.standard.string(forKey: "cartsItems") print(count)

When i come back to vc1 from vc2 through back button value never update and when i call some other vc and than call again vc1 it gets update. How can i update value at that time?

最满意答案

那么,正确的做法是使用简单的代理协议将数据从一个VC2传输到另一个VC1 。

以下是如何使用protocol的步骤。

在VC2 `执行以下步骤。

就在你的类声明之上宣告你的委托。

protocol MyDelegate:class { func sendDataBack(value: Int) }

在class声明你的delegate一个weak variable

weak var myDelegateObj: MyDelegate?

当你dismiss the VC只需要用线呼叫委托人

myDelegateObj?.sendDataBack(value: yourIntegrerValue)

现在转到您的VC1并转到您已将VC1推入VC2并执行以下操作。

vc2.myDelegateObj = self // vc2 is VC2 objcet

并在VC1实施您的委托方法

func sendDataBack(value: Int) { print(value) }

希望这可以帮助。

Well, the proper way to do this is with a simple delegate protocol to transfer data from one VC2 to another VC1.

Below are the steps how to work with the protocol.

In VC2 ` do the following steps.

Just above your class declaration declare your delegate.

protocol MyDelegate:class { func sendDataBack(value: Int) }

In class declare a weak variable of your delegate

weak var myDelegateObj: MyDelegate?

and when you dismiss the VC just call the delegate with line

myDelegateObj?.sendDataBack(value: yourIntegrerValue)

Now go to your VC1 and go to the line where you have pushed VC1 to VC2 and do the following.

vc2.myDelegateObj = self // vc2 is VC2 objcet

and implement the method your delegate in the VC1

func sendDataBack(value: Int) { print(value) }

Hope this helps.

更多推荐

本文发布于:2023-07-24 00:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239122.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UserDefaults   Swift   updating   Issue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!