这是我的第一个Swift / Firestore练习应用程序,我正在尝试访问“ EventTableViewController”文件中要使用的“ AddEventViewController”文件中的变量(eventLocation,eventDate)。
但是我似乎无法弄清楚丢失了什么,或者我做错了。
将感谢经验丰富的人的帮助。提前非常感谢!
EventTableViewController.swift
import UIKit
import FirebaseFirestore
class EventsTableViewController: UITableViewController {
var eventsArray = [Event]()
override func viewDidLoad() {
super.viewDidLoad()
loadData()
}
func loadData() {
// How to pass variables from 'AddEventViewController' and use it inside this function??
Firestore.firestore().collection("Locations").document(??).collection("Dates").document(??).collection("Events").getDocuments() { (querySnapshot, error) in
if let error = error {
print("Error getting documents: \(error)")
} else {
self.eventsArray = querySnapshot!.documents.compactMap({Event(dictionary: $0.data())})
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return eventsArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell", for: indexPath)
let event = eventsArray[indexPath.row]
// Configure the cell...
cell.textLabel?.text = "\(event.programType)"
cell.detailTextLabel?.text = "\(event.eventLocation) \(event.eventDate)"
return cell
}
AddEventViewController.swift
import UIKit
import FirebaseFirestore
class AddEventViewController: UIViewController {
@IBOutlet weak var eventLocation: UITextField!
@IBOutlet weak var eventDate: UITextField!
@IBOutlet weak var programType: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func saveEventButtonTapped(_ sender: UIBarButtonItem) {
// Check if textfields are not empty
guard let EventLocation = eventLocation.text, !EventLocation.isEmpty else { return }
guard let EventDate = eventDate.text, !EventDate.isEmpty else { return }
guard let Program = programType.text, !Program.isEmpty else {return}
**// need to find a way to pass variables i.e. eventLocation and eventDate to 'EventsTableViewController'**
// Save profile details of person to Firestore as data fields
let eventDetail: [String: Any] = ["Event Location": EventLocation, "Event Date": EventDate, "Program": Program]
// Write data to Firestore
Firestore.firestore().collection("Locations").document(EventLocation).collection("Dates").document(EventDate).collection("Events").document(Program).setData(eventDetail)
{ error in
if let error = error {
print("ERROR: Event not saved to database. Please try again! \(error)")
} else {
print("Event has been saved to the database!")
}
self.dismiss(animated: true, completion: nil)
}
}
Event.swift
struct Event {
var programType: String
var eventLocation: String
var eventDate: String
var dictionary: [String: Any] {
return [
"programType": programType,
"eventLocation" : eventLocation,
"eventDate" : eventDate
]
}
}
extension Event : DocumentSerializable {
init?(dictionary: [String: Any]) {
guard let programType = dictionary["Program"] as? String,
let eventLocation = dictionary["Event Location"] as? String,
let eventDate = dictionary["Event Date"] as? String else { return nil}
self.init(programType: programType, eventLocation: eventLocation, eventDate: eventDate)
}
}
答案 0 :(得分:0)
有一种方法可以将loadData()
添加到viewWillAppear
方法中,如下所示:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadData()
}
并将其从viewDidLoad
中删除,因为viewDidLoad
仅在加载视图时调用一次。但是viewWillAppear
会在您每次访问视图时调用。因此,每次您返回上一个视图时,它将从服务器获取新数据。