0

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?

Roshan
  • 712
  • 4
  • 17
Awais
  • 89
  • 1
  • 2
  • 16

2 Answers2

1

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.

Roshan
  • 712
  • 4
  • 17
  • let me try. @Roshan Singh Bisht – Awais May 16 '18 at 12:50
  • let me know if any confusions! – Roshan May 16 '18 at 12:57
  • where should i declare this in vc1? vc2.myDelegateObj = self . @Roshan Singh Bisht – Awais May 16 '18 at 13:40
  • declare it where you have taken the vc2 object in vc1 or where you trying to push from vc1 to vc2 – Roshan May 16 '18 at 16:23
  • i have declared there but it crashes on that point where i declare that on my push view controller. @Roshan Singh Bisht – Awais May 17 '18 at 03:08
  • This is what i write in my vc1 let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewController(withIdentifier: "RestaurantMenuDetailVC") as! RestaurantMenuDetailVC vc.categoryModel = categoryModel vc.subCategoryModel = subCategoryModel vc.AddonCategoryModel = AddonCategoryModel vc.myDelegateObj = self as! MyDelegate self.navigationController?.pushViewController(vc, animated: true) . @Roshan Singh Bisht – Awais May 17 '18 at 03:58
  • you don't need to do this `vc.myDelegateObj = self as! MyDelegate ` instead do this the `class ViewController: UIViewController, MyDelegate ` and just declare `vc.myDelegateObj = self` Means you need to make your `ViewController` conform to `MyDelegate` by doing this `class ViewController: UIViewController,MyDelegate` – Roshan May 17 '18 at 04:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171196/discussion-between-roshan-singh-bisht-and-awais). – Roshan May 17 '18 at 04:06
  • i got the value thanks a lot :) . @Roshan Singh Bisht – Awais May 17 '18 at 04:28
  • do upvote the answer if you find it useful in anyway, thanks in advance :) – Roshan May 17 '18 at 15:21
0

I am trying to do something similar to what you are doing. I am translating the app to other languages (localization). So when I choose one language on a VC2, I go back to VC1 and the strings in VC1 should update according to the language choosen.

The language is stored in Userdefaults().

I think I know why you are having this problem. To realize what is happening try to write this in VC1:

let count =  UserDefaults.standard.string(forKey: "cartsItems") // I suppose this is a global variable.
override func viewWillAppear(_ animated: Bool) {

    print("This is the value of the global variable: \(count)")
    print("This is the value of Userdefaults directly: \(UserDefaults.standard.string(forKey: "cartsItems"))")     

}

You would expect both results to be the same. However, the first one, from the global variable, will be not updated; whereas the second one, using Userdefaults directly, is updated.

I suppose this is because when you load VC1, the view is loaded and the variables get their values. When you open VC2, it opens above VC1. When you go back to VC1 you are not loading it again, but going back to what is already loaded. And the global variable remains with the same value as in the beginning.

Hope that helps.

jRuMoL
  • 349
  • 1
  • 12