简体   繁体   中英

Swift: Call a method from ViewController1 after dismissing ViewController2

I'm currently building a homework tracking app where you can add courses in a TableView. In one ViewController, I have a list of courses that already exist. I also have a button that allows the user to add new courses. When they click the button, the app triggers a modal segue to a new ViewController where they can fill out a form to add a new course. However, when they finish and click the button that dismisses the current ViewController to go back to the courses list, I can't find a way of updating the courses list with the course that the user just added. I know that if using a segue, you can use the prepare method. However I am calling

 dismiss(animated: true, completion: nil)

The method that I want to call in order to reload the table is in the first ViewController. Is there a way to call the load method in the first ViewController before or after the second ViewController has dismissed?

As per your requirement, a simple "delegation" will work.

Step 1: Define a protocol.

protocol ViewController2Delegate: class {
func refresh()
}

Step2: Create a delegation at ViewController2

weak var delegation: ViewController2Delegate?

Step3: As you are using "segue" from a button to create ViewController2 from ViewController1, use prepareForSegue method in ViewController1 and set that ViewController1 is conforming the delegate. ViewController1 will conform the delegate and reload the table.

Step4: In ViewController2, on tap of doneButton, call delegate?.refresh() as per your requirement (before dismissing / after dismissing of viewcontroller2 - use completionBlock of dismiss() method).

you can use viewWillappear function in first view controller to refresh or just using completion handler as next:

1) in the second controller add this variable.

var completion: (() -> Void)?

2) in the first controller before showing the second controller you need to add this code.

let controller = secondController()
controller.completion = {
  // here you can refresh your first controller
}

3) to make the refresh here is the last step you need to do in the second controller before you call the dismiss function.

if let completion = completion{
  completion()
}

You can call reloadData in viewWillAppear on ViewController1

override func viewWillAppear(_ animated: Bool) {
        tableview.reloadData() 
}

You could do the loadData or refreshData on viewWillAppear of the FirstViewController , like this:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    refresData()
}

The viewWillAppear of FirstViewController will be called right after you call to dismiss from the SecondViewController .

You can use a delegate for the first controller in you second view controller and call it before you call the dismiss. If you want to to run after the dismiss, then you can call it in the completion of the dismiss function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM