Swift: Reload a View Controller

后端 未结 8 963
南旧
南旧 2020-12-15 02:41

I need to refresh a view controller when a certain button is tapped. Simply activating viewDidLoad() does not seem to be working for what I need to

8条回答
  •  星月不相逢
    2020-12-15 03:30

    You shouldn't call viewDidLoad method manually, Instead if you want to reload any data or any UI, you can use this:

    override func viewDidLoad() {
        super.viewDidLoad();
    
        let myButton = UIButton()
    
        // When user touch myButton, we're going to call loadData method
        myButton.addTarget(self, action: #selector(self.loadData), forControlEvents: .TouchUpInside)
    
        // Load the data
        self.loadData();
    }
    
    func loadData() {
        // code to load data from network, and refresh the interface
        tableView.reloadData()
    }
    

    Whenever you want to reload the data and refresh the interface, you can call self.loadData()

提交回复
热议问题