Force refresh on another ViewController component with swift

旧时模样 提交于 2019-12-11 01:10:43

问题


I'm trying to pass a trigger between 2 view controller but i can't find something that works ...

I have the main controller with a NSTableView, source linked, View Base.

main Controller class :

class ViewController: NSViewController {

I have an array defined in Global variables. on the viewDidLoad i add 2 elements in my array, then i use setDelegate and setDataSource. It works fine.

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

I have a segue from the WINDOWS CONTROLLER to my second view Controller. (It's a ToolBar button). The segue is "Sheet" kind.

This second controller allows me to add an element in my global variable arrays, with a button "Save" .

Code on the second controller

@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 reloadData on the TableView, it works fine. The third element is added.

but i would like that to be automatic ....

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

Can you please help ?

Thanks, Nicolas


回答1:


I think, you want to add data from oneViewController and without pressing button, you want after navigation or pressing back button, you want to reload automatically tableview to show updated results, right.

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

Follow this below steps:

Step 1 :

Add this code in your view controller, from which you want to add data in array or in database, instead of button click.

 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 its final step. Now add this below code, to your result view 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-stuff in your coding. Hope it works for you.




回答2:


If you want the table to reload automatically when the View appears add this to viewWillAppear

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


来源:https://stackoverflow.com/questions/39363388/force-refresh-on-another-viewcontroller-component-with-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!