首页 > 解决方案 > 将访问从 tableViewController 传递到另一个视图控制器

问题描述

我在这上面花了相当多的时间,但似乎仍然无法弄清楚我做错了什么。

我想在我自己的函数中在 tableViewController 之外的另一个视图控制器上运行 reloadData() 函数,但我收到错误类型“HomeViewController”没有成员“reloadData”。

主视图控制器:

import UIKit
import CoreData

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var index = ""
    var item : [ListItem] = [] //listName
    var listName = [""]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.homeListsTableView.delegate = self
        self.homeListsTableView.dataSource = self
        homeListsTableView.reloadData()

        //List Names

        //List Items - Within a list Name

        let listItem1 = ListItem()
        listItem1.name = "" //Update so names are updated via append the ListItem Array
        listItem1.location = ""
        item.append(listItem1)

        let listItem2 = ListItem()
        listItem2.name = ""
        listItem2.location = ""
        item.append(listItem2)

    }


    @IBOutlet weak var homeListsTableView: UITableView!

    @IBAction func templatesButton(_ sender: Any) {

        tabBarController?.selectedIndex = 2
    }

   override func viewWillAppear(_ animated: Bool) {
             if let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext {
                 if let coreDataListItems = try? context.fetch(ListName.fetchRequest()) as? [ListName] {
                     print(coreDataListItems)
                 }
             }
         }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listName.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = listName[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // performSegue(withIdentifier: "goToItems", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let vc = segue.destination as! ListNameViewController
        vc.homeListsTableViewVC = homeListsTableView

    }

}

ListName 视图控制器:

import UIKit
import CoreData

class ListNameViewController: UIViewController, UITableViewDelegate {


   // var listName = [""]
    let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext
    var homeListsTableViewVC = HomeViewController.self

    override func viewDidLoad() {
        super.viewDidLoad()

}

@IBOutlet weak var listNameValue: UITextField!
    @IBOutlet weak var locationOption: UITextField!
    @IBOutlet weak var createButtonChange: UIButton!

 @IBAction func createButton(_ sender: Any) {

        let newList = ListName(context: context!)
        newList.listName = listNameValue.text

        // let location = locationOption.text!
        //tabBarController?.selectedIndex = 0
        //performSegue(withIdentifier: "home", sender: nil)

    }

    func saveList() {

          do {
            try context!.save()
          } catch {
            print("Error saving context \(error)")
        }
        homeListsTableViewVC.reloadData()
      }
}

我正在尝试使用 prepare for segue 函数来传递 homeListsTableView 数据,然后使用 reloadData() 函数。有人可以告诉我我做错了什么吗?

谢谢!

标签: swiftuitableviewsegue

解决方案


您可以使用委托或闭包来更新您的表格视图。这里是如何在您的类中使用闭包来更新视图的指针

class ListNameViewController: UIViewController, UITableViewDelegate {
    var callback: (()->())?
    //....

    func saveList() {

        do {
          try context!.save()
        } catch {
          print("Error saving context \(error)")
      }
      callback?()
    }
}

在创作的同时ListNameViewController

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let vc = segue.destination as! ListNameViewController
        vc.callback = {  [weak self] in
            self?.homeListsTableView.reloadData()
        }

    }
}

推荐阅读