将数据从文本字段传递到Firebase

时间:2016-12-29 12:28:01

标签: ios swift

我在ToDoViewController.Swift中收到错误这里是整体代码: 我正在制作一个简单的应用程序来接受客户的订单。并且订单将存储在Firebase中。

查看Controller.Swift

import UIKit
import Firebase

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var todoList = [Todo]()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }

    func loadData() {
        self.todoList.removeAll()
        let ref = FIRDatabase.database().reference()
        ref.child("todoList").observeSingleEvent(of: .value, with: { (snapshot) in
            if let todoDict = snapshot.value as? [String:AnyObject] {
                for (_,todoElement) in todoDict {
                    print(todoElement);
                    let todo = Todo()
                    todo.name = todoElement["name"] as? String
                    todo.message = todoElement["message"] as? String
                    todo.reminderDate = todoElement["date"] as? String
                    self.todoList.append(todo)
                }
            }
            self.tableView.reloadData()

        }) { (error) in
            print(error.localizedDescription)
        }

    }

    //MARK: TableView datasource

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoCell")
        cell!.textLabel?.text = todoList[indexPath.row].name
        return cell!
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }
}

ToDoViewController

import UIKit
import Firebase

class ToDoViewController: UIViewController {

    var todo:Todo?

    @IBOutlet weak var nameField: UITextField!
    @IBOutlet weak var messageField: UITextField!
    @IBOutlet weak var dateFormatter: UIDatePicker!

    @IBAction func Done(_ sender: Any) {
        if todo == nil {
            todo = Todo()
        }

        // first section
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd/MM/yyyy hh:mm"

        todo?.name = self.nameField.text
        todo?.message = self.messageField.text
        todo?.reminderDate = dateFormatter.string(from: self.dateFormatter.date)

        //second section
        let ref = FIRDatabase.database().reference()
        let key = ref.child("todoList").childByAutoId().key

        let dictionaryTodo = [ "name"    : todo!.name! ,
                               "message" : todo!.message!,
                               "date"    : todo!.reminderDate!]

        let childUpdates = ["/todoList/\(key)": dictionaryTodo]
        ref.updateChildValues(childUpdates, withCompletionBlock: { (error, ref) -> Void in
            self.navigationController?.popViewController(animated: true)
        })
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let todoVC = self.storyboard!.instantiateViewController(withIdentifier: "ToDoVC") as! ToDoViewController
        todoVC.todo = todoList[indexPath.row]
        self.navigationController?.pushViewController(todoVC, animated: true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        if self.todo != nil {
            nameField.text = self.todo?.name
            messageField.text = self.todo?.message
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "dd/MM/yyyy hh:mm"
            let date = dateFormatter.date(from: self.todo!.reminderDate!)
            datePicker.date = date!
        }
    }
}

Todo.Swift

import UIKit
class Todo: NSObject {
    var name :String?
    var message: String?
    var reminderDate: String?
    // id which is set from firebase to uniquely identify it
    var uniqueId:String?
}

enter image description here

1 个答案:

答案 0 :(得分:0)

您的代码有问题:

  1. $fees_rules_comp_sheet= input::all(); foreach ($fees_rules_comp_sheet as $fees_comp_sheet) { $fees_comp_sheet = new FeesCompSheet(); $fees_comp_sheet->fees_comp_sheet_id = $this->getFeesCompSheetId(); $fees_comp_sheet->max_interval_amount = Input::get('max_interval_amount'); $fees_comp_sheet->law5_fixed_amount = Input::get('law5_fixed_amount'); $fees_comp_sheet->law5_rate = Input::get('law5_rate'); $fees_comp_sheet->law5_min_amount = Input::get('law5_min_amount'); $fees_comp_sheet->law5_max_amount = Input::get('law5_max_amount'); $fees_comp_sheet->law5_grace_amount = Input::get('law5_grace_amount'); $fees_comp_sheet->save();
  2. Use of un-resolved identifier 'todoList'todoList中声明,您无法在View Controller.Swift中使用它。

    1. ToDoViewController
    2. 代码Cannot assign to property: 'date' is a method未定义且datePicker应为:

      @IBOutlet weak var dateFormatter: UIDatePicker!

      AND图片@IBOutlet weak var datePicker: UIDatePicker! 应为:

      dateFormatter.date = date!