检查来自另一个 ViewController 的按钮
Posted
技术标签:
【中文标题】检查来自另一个 ViewController 的按钮【英文标题】:Check button from another ViewController 【发布时间】:2017-05-09 08:41:02 【问题描述】:我正在尝试一个事件应用程序。我可以将事件添加到 Firebase 数据库并检索它们。当我从 viewController 单击添加按钮时,事件的信息将添加到 Firebase 数据库。但在我注销并重新登录之前,它无法显示在我的 tableViewController 上。
这是我的检索代码:
private func loadPlaces()
let ref = FIRDatabase.database().reference()
ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: snapshot in
let images = snapshot.value as! [String : AnyObject]
// self.places.removeAll()
for (_, value) in images
let userToShow = events()
if let img = value["pathToImage"] as? String,
let eventDate = value["event date"] as? String,
let name = value["event name"] as? String
//let information = value["information"] as? String
// self.places.append(historicalPlaces(img: img , name: name2, inf:information))
userToShow.eventImages = img
userToShow.eventDates = eventDate
userToShow.eventnames = name
// userToShow.information = information
self.event.append(userToShow)
self.tableView.reloadData()
)
// ref.removeAllObservers()
这是我的uploadPost代码:
import UIKit
import os.log
import Firebase
class UploadPost: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
@IBOutlet weak var eventImage: UIImageView!
@IBOutlet weak var selectImageBtn: UIButton!
@IBOutlet weak var eventName: UITextField!
@IBOutlet weak var eventInf: UITextView!
@IBOutlet weak var addBtn: UIButton!
@IBOutlet weak var eventDate: UITextField!
@IBOutlet weak var addEvent: UIBarButtonItem!
var picker = UIImagePickerController()
let datePicker = UIDatePicker()
override func viewDidLoad()
super.viewDidLoad()
picker.delegate = self
createDatePicker()
func createDatePicker()
//format for picker
datePicker.datePickerMode = .dateAndTime
// toolbar
let toolbar = UIToolbar()
toolbar.sizeToFit()
// bar button item
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([doneButton], animated: false)
eventDate.inputAccessoryView = toolbar
//assigning date picker to text field
eventDate.inputView = datePicker
func donePressed()
//format date
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .short
eventDate.text = dateFormatter.string(from: datePicker.date)
self.view.endEditing(true)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
if let image = info[UIImagePickerControllerEditedImage] as? UIImage
self.eventImage.image = image
selectImageBtn.isHidden = true
addBtn.isHidden = false
self.dismiss(animated: true, completion: nil)
@IBAction func selectImage(_ sender: Any)
picker.allowsEditing = true
picker.sourceType = .photoLibrary
self.present(picker, animated: true, completion: nil)
@IBAction func addEventPressed(_ sender: Any)
@IBAction func addPressed(_ sender: Any)
AppDelegate.instance().showActivityIndicator()
let uid = FIRAuth.auth()!.currentUser!.uid
let ref = FIRDatabase.database().reference()
let storage = FIRStorage.storage().reference(forURL: "gs://bauventdemo.appspot.com")
let key = ref.child("events").childByAutoId().key
let imageRef = storage.child("events").child(uid).child("\(key).jpg")
let data = UIImageJPEGRepresentation(self.eventImage.image!, 0.6)
let uploadTask = imageRef.put(data!, metadata: nil) (metadata, error) in
if error != nil
print(error!.localizedDescription)
AppDelegate.instance().dismissActivityIndicatos()
return
imageRef.downloadURL(completion: (url, error) in
if let url = url
let feed = ["userID" : uid,
"pathToImage" : url.absoluteString,
"event name" : self.eventName.text!,
"event date" : self.eventDate.text!,
"author" : FIRAuth.auth()!.currentUser!.displayName!,
"event information" : self.eventInf.text!,
"postID" : key] as [String : Any]
let postFeed = ["\(key)" : feed]
ref.child("posts").updateChildValues(postFeed)
AppDelegate.instance().dismissActivityIndicatos()
self.dismiss(animated: true, completion: nil)
)
uploadTask.resume()
我的代码仅在我登录时检查数据库,所以如何在添加新事件时检查 firebase 数据库并检索它们?
【问题讨论】:
【参考方案1】:它不更新表的原因是因为您试图在后台线程中重新加载表。 相反,在主线程中重新加载它。像这样:
DispatchQueue.main.async
self.tableView.reloadData()
更新代码
private func loadPlaces()
let ref = FIRDatabase.database().reference()
ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: snapshot in
let images = snapshot.value as! [String : AnyObject]
// self.places.removeAll()
for (_, value) in images
let userToShow = events()
if let img = value["pathToImage"] as? String,
let eventDate = value["event date"] as? String,
let name = value["event name"] as? String
//let information = value["information"] as? String
// self.places.append(historicalPlaces(img: img , name: name2, inf:information))
userToShow.eventImages = img
userToShow.eventDates = eventDate
userToShow.eventnames = name
// userToShow.information = information
self.event.append(userToShow)
DispatchQueue.main.async
self.tableView.reloadData()
)
// ref.removeAllObservers()
【讨论】:
谢谢,但我找不到可以在我的代码中添加该代码的位置。你能告诉我吗?func loadPlaces()
的最后一行,你是在做self.tableview.reload()
,而不是那样做。【参考方案2】:
我尝试添加从数据库中检索数据的刷新按钮作为解决方法。
@IBAction func refresh(_ sender: Any)
loadPlaces()
我把这段代码用来防止重复事件。
self.event.removeAll()
【讨论】:
以上是关于检查来自另一个 ViewController 的按钮的主要内容,如果未能解决你的问题,请参考以下文章
如何从 SWIFT 3 中的另一个 ViewController 检查 UISwitch 的状态?
iOS:如何检查一个变量是不是在 dispatch_async 的另一个 ViewController 中改变了他的值?
从另一个 ViewController 调用一个 ViewController 的实例方法