[英]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.我目前正在构建一个家庭作业跟踪应用程序,您可以在其中在 TableView 中添加课程。 In one ViewController, I have a list of courses that already exist.
在一个 ViewController 中,我有一个已经存在的课程列表。 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.
当他们单击按钮时,应用程序会触发模态搜索到新的 ViewController,他们可以在其中填写表单以添加新课程。 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.
但是,当他们完成并单击将当前 ViewController 关闭到 go 的按钮返回课程列表时,我找不到使用用户刚刚添加的课程更新课程列表的方法。 I know that if using a segue, you can use the prepare method.
我知道如果使用 segue,您可以使用 prepare 方法。 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.我要调用以重新加载表的方法位于第一个 ViewController 中。 Is there a way to call the load method in the first ViewController before or after the second ViewController has dismissed?
有没有办法在第二个 ViewController 关闭之前或之后调用第一个 ViewController 中的 load 方法?
As per your requirement, a simple "delegation" will work.根据您的要求,一个简单的“委托”将起作用。
Step 1: Define a protocol.第 1 步:定义协议。
protocol ViewController2Delegate: class {
func refresh()
}
Step2: Create a delegation at ViewController2 Step2:在 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.步骤 3:当您使用按钮中的“segue”从 ViewController1 创建 ViewController2 时,使用 ViewController1 中的
prepareForSegue
方法并设置 ViewController1 符合委托。 ViewController1 will conform the delegate and reload the table. ViewController1 将符合委托并重新加载表。
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).第4步:在ViewController2中,点击doneButton,根据您的要求调用
delegate?.refresh()
(在关闭viewcontroller2之前/关闭viewcontroller2之后-使用dismiss()方法的completionBlock)。
you can use viewWillappear function in first view controller to refresh or just using completion handler as next:您可以在第一个视图 controller 中使用 viewWillappear function 来刷新或仅使用完成处理程序如下:
1) in the second controller add this variable. 1) 在第二个 controller 添加这个变量。
var completion: (() -> Void)?
2) in the first controller before showing the second controller you need to add this code. 2) 在第一个 controller 显示第二个 controller 之前,您需要添加此代码。
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. 3)在此处进行刷新是您在调用驳回 function 之前需要在第二个 controller 中执行的最后一步。
if let completion = completion{
completion()
}
You can call reloadData in viewWillAppear on ViewController1您可以在 ViewController1 上的 viewWillAppear 中调用 reloadData
override func viewWillAppear(_ animated: Bool) {
tableview.reloadData()
}
You could do the loadData
or refreshData
on viewWillAppear
of the FirstViewController
, like this:您可以在
FirstViewController
的viewWillAppear
上执行loadData
或refreshData
,如下所示:
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
. FirstViewController
的viewWillAppear
将在您调用以从SecondViewController
中关闭后立即调用。
You can use a delegate for the first controller in you second view controller and call it before you call the dismiss.您可以在第二个视图 controller 中为第一个 controller 使用委托,并在调用解雇之前调用它。 If you want to to run after the dismiss, then you can call it in the completion of the dismiss function.
如果要在dismiss后运行,那么可以在dismiss完成后调用它function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.