我希望我的应用程序的此活动获取输入到UItextfield中的内容并将其保存到Firebase。
当我运行我的应用程序时,Xcode没有显示错误,但是文本没有上传到firebase

import UIKit
import Firebase

class Admin_events: UIViewController {

@IBOutlet weak var event_text: UITextField!

var ref : DatabaseReference!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    ref=Database.database().reference()
}
@IBAction func sendtofirebase(_ sender: Any) {
    let event = event_text.text
ref?.child("Events").setValue(event)
    NSLog("Uploading...")
}
}

最佳答案

didReceiveMemoryWarning仅在系统内存不足时调用。在大多数情况下,甚至根本不会调用它,这意味着您的ref属性将保持为零。
尝试在其他地方设置属性。viewDidLoad在视图控制器的生命周期中很早就被调用(而且只调用一次),因此它是进行此类设置的好地方。

import UIKit
import Firebase

class Admin_events: UIViewController {

    @IBOutlet weak var event_text: UITextField!

    var ref : DatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        // One-time setup is commonly done here.
        // This is a good time to create your database reference.

        ref = Database.database().reference()
    }

    @IBAction func sendtofirebase(_ sender: Any) {
        let event = event_text.text
        ref?.child("Events").setValue(event)
        NSLog("Uploading...")
    }
}

07-28 07:40