2016-12-29 60 views
-1

我在ToDoViewController.Swift中收到錯誤,這裏是整體代碼: 我正在製作一個簡單的應用程序,用於接收來自客戶的訂單。訂單將存儲在Firebase中。將數據從文本字段傳遞到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

回答

0

東西是你的代碼錯誤:

  1. Use of un-resolved identifier 'todoList'

todoListView Controller.Swift聲明並不能在ToDoViewController使用它。

  • Cannot assign to property: 'date' is a method
  • 在代碼datePicker是未定義和@IBOutlet weak var dateFormatter: UIDatePicker!應該是:

    @IBOutlet weak var datePicker: UIDatePicker! 
    

    並且在圖像dateFormatter.date = date!應該是:

    datePicker.date = date! 
    
    相關問題