0

My question is how to update or reload ViewController when cell selected in UITableView. I have ViewController with two buttons and when you click on one of them you will go to tableViewController and when you select one option, the option will be saved by

NSUserDefaults.standardUserDefaults()

and tableViewController will dismiss by

self.dismissViewControllerAnimated(true, completion: nil)

. I used

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = self.tableView.cellForRowAtIndexPath(indexPath)

    let lang_name = cell!.textLabel!.text!
        NSUserDefaults.standardUserDefaults().setObject(lang_name,forKey:"lang_saved")
    NSUserDefaults.standardUserDefaults().synchronize()

    self.dismissViewControllerAnimated(true, completion: nil)     
}

and

super.viewDidLoad()
if let lang_saved = NSUserDefaults.standardUserDefaults().objectForKey("lang_saved") {
     button1.setTitle("\(lang_saved)", forState: .Normal)
}

My problem is I must restart my app to get right result, I mean that the viewcontroller must be reloaded to get button name from

NSUserDefaults.standardUserDefaults()

how can I solve that?

Yunus Eren Güzel
  • 3,018
  • 11
  • 36
  • 63
J.S.O
  • 111
  • 1
  • 13

2 Answers2

0

To Reload you button, you must call this code again, when you want to update the button ( it may be on the click, or in the viewWillAppear function )

        if let lang_saved = NSUserDefaults.standardUserDefaults().objectForKey("lang_saved") {
             button1.setTitle("\(lang_saved)", forState: .Normal)
        }

The problem with your code is you call only on viewDidLoad and this execute only ones when your view is loaded.

Ulysses
  • 2,281
  • 1
  • 16
  • 24
  • thank you, but I don't want to reload a table view , I want to reload view controller which I came from which have buttons I want to update – J.S.O Mar 05 '16 at 16:14
  • wow, in that case just call the query from NSUserDefault again. I will rewrite the answer. – Ulysses Mar 05 '16 at 16:15
0

Check this link: Link

You should put your reload code in:

// Define Delegate Method
func childViewControllerResponse(parameter)
{
   .... // self.parameter = parameter
}

OR

Maybe call

if let lang_saved = NSUserDefaults.standardUserDefaults().objectForKey("lang_saved") {

        button1.setTitle("\(lang_saved)", forState: .Normal)
    }

in viewWillAppear

Community
  • 1
  • 1
Oscar J. Irun
  • 455
  • 5
  • 17