简体   繁体   English

Swift:关闭 ViewController2 后从 ViewController1 调用方法

[英]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:您可以在FirstViewControllerviewWillAppear上执行loadDatarefreshData ,如下所示:

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 . FirstViewControllerviewWillAppear将在您调用以从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.

相关问题 如何从 viewcontroller1 (Swift) 在 vi​​ewcontroller2 中附加列表和重新加载 tableView - How to append list and reload tableView in viewcontroller2 from viewcontroller1 (Swift) 将字符串从viewcontroller1传输到viewcontroller2并在uiwebview的文本框中显示条形码扫描(字符串) - Transfer String from viewcontroller1 to viewcontroller2 and show barcode scan(String) in textbox of uiwebview 崩溃:在viewController1上旋转时约束在viewController2中为零 - Crash: constraint is nil in viewController2 while rotating on viewController1 如何在基于页面的应用程序(PageViewController)中单击按钮时从ViewController1移至ViewController2 - How to move from ViewController1 to ViewController2 on a button click in a page based application (PageViewController) 如何使用从ViewController2传回的数据更新ViewController1数据 - How do I update ViewController1 data with data passed back from ViewController2 如何防止对象被迅速覆盖? ViewController1中的对象被ViewController2中相同类型的对象覆盖 - How to prevent object being overwritten - swift ? Object in ViewController1 is being over-written with same type of object in ViewController2 Swift:解除模态后在ViewController上执行一个函数 - Swift: Perform a function on ViewController after dismissing modal Swift - 在关闭 Modal ViewController 后重新加载 TableView - Swift - Reload TableView After Dismissing Modal ViewController Swift - 关闭 ViewController 后停止 TableView 重新加载 - Swift - Stop TableView Reloading After Dismissing A ViewController 使用 viewDidAppear() 在 swift5 中关闭另一个 ViewController 后刷新 ViewController - Refreshing ViewController after dismissing another ViewController in swift5 using viewDidAppear()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM