Force update on another ViewController component with fast

I am trying to pass a trigger between two view controllers, but I can not find something that works ...

I have a main controller with an NSTableView associated with the source, Base Base.

controller main class:

class ViewController: NSViewController { 

I have an array defined in global variables. on viewDidLoad, I add 2 elements to my array, then I use setDelegate and setDataSource. It works great.

 myLib.myCoins = fillCoinsTable() print ("My lib has \(myLib.myCoins.count) objects ") mainCoinTable.setDelegate(self) mainCoinTable.setDataSource(self) 

I have a segue from WINDOWS CONTROLLER on my second controller. (This is the ToolBar button). Segue is a sheet type.

This second controller allows me to add an item to my global variable arrays using the "Save" button.

Second Controller Code

 @IBAction func addCoinButton(sender: AnyObject) { //We add the coin let newCoin:coin = coin(n: textName.stringValue) newCoin.Year = Int.init(textYear.stringValue) myLib.myCoins.append(newCoin) self.dismissController(self) } 

If I add a button on the main controller to reload Data in the TableView, it works fine. The third item has been added.

but I would like it to be automatic ....

I tried segue, but mine is not between the view controller, but with the Windows controller.

Can you help?

Thanks Nicolas

+3
source share
2 answers

I think you want to add data from oneViewController and without clicking the button you want after navigating or clicking the button, you want to automatically restart the tableview to show the updated results correctly.

If I'm not mistaken, this solution will work for you.

Follow these steps:

Step 1:

Add this code to your controller from which you want to add data to an array or database, instead of clicking a button.

  override func viewDidLoad() { super.viewDidLoad() let backItem = UIBarButtonItem(title: "Back", style: .Plain, target: self, action: "goBack") self.navigationItem.leftBarButtonItem = backItem } func goBack() { NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil) self.navigationController?.popViewControllerAnimated(true) } 

Step 2:

Now is his last step. Now add this code below to the result controller where you want to automatically update the result.

  func loadList(notification: NSNotification){ self.TableView.reloadData() } override func viewWillAppear(animated: Bool) { NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:",name:"load", object: nil) } 

Now implement this code in your own encoding. Hope this works for you.

+3
source

If you want the table to automatically reload when the view appears, add this value to viewWillAppear

 self.tableView.performSelectorOnMainThread(#selector(UITableView.reloadData), withObject: nil, waitUntilDone: true) 
+1
source

Source: https://habr.com/ru/post/1014839/


All Articles